|
| 1 | +/* |
| 2 | +https://github.com/peterix/dfhack |
| 3 | +Copyright (c) 2011 Petr Mrázek <peterix@gmail.com> |
| 4 | +
|
| 5 | +A thread-safe logging console with a line editor for windows. |
| 6 | +
|
| 7 | +Based on linenoise win32 port, |
| 8 | +copyright 2010, Jon Griffiths <jon_p_griffiths at yahoo dot com>. |
| 9 | +All rights reserved. |
| 10 | +Based on linenoise, copyright 2010, Salvatore Sanfilippo <antirez at gmail dot com>. |
| 11 | +The original linenoise can be found at: http://github.com/antirez/linenoise |
| 12 | +
|
| 13 | +Redistribution and use in source and binary forms, with or without |
| 14 | +modification, are permitted provided that the following conditions are met: |
| 15 | +
|
| 16 | + * Redistributions of source code must retain the above copyright notice, |
| 17 | + this list of conditions and the following disclaimer. |
| 18 | + * Redistributions in binary form must reproduce the above copyright |
| 19 | + notice, this list of conditions and the following disclaimer in the |
| 20 | + documentation and/or other materials provided with the distribution. |
| 21 | + * Neither the name of Redis nor the names of its contributors may be used |
| 22 | + to endorse or promote products derived from this software without |
| 23 | + specific prior written permission. |
| 24 | +
|
| 25 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 29 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 | +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | +POSSIBILITY OF SUCH DAMAGE. |
| 36 | +*/ |
| 37 | + |
| 38 | + |
| 39 | +#include <stdarg.h> |
| 40 | +#include <errno.h> |
| 41 | +#include <stdio.h> |
| 42 | +#include <iostream> |
| 43 | +#include <fstream> |
| 44 | +#include <istream> |
| 45 | +#include <string> |
| 46 | + |
| 47 | +#include "ColorText.h" |
| 48 | +#include "MiscUtils.h" |
| 49 | + |
| 50 | +#include <cstdio> |
| 51 | +#include <cstdlib> |
| 52 | +#include <sstream> |
| 53 | + |
| 54 | +using namespace DFHack; |
| 55 | + |
| 56 | +#include "tinythread.h" |
| 57 | +using namespace tthread; |
| 58 | + |
| 59 | +void color_ostream::flush_buffer(bool flush) |
| 60 | +{ |
| 61 | + auto buffer = buf(); |
| 62 | + auto str = buffer->str(); |
| 63 | + |
| 64 | + if (!str.empty()) { |
| 65 | + add_text(cur_color, buffer->str()); |
| 66 | + buffer->str(std::string()); |
| 67 | + } |
| 68 | + |
| 69 | + if (flush) |
| 70 | + flush_proxy(); |
| 71 | +} |
| 72 | + |
| 73 | +void color_ostream::begin_batch() |
| 74 | +{ |
| 75 | + flush_buffer(false); |
| 76 | +} |
| 77 | + |
| 78 | +void color_ostream::end_batch() |
| 79 | +{ |
| 80 | + flush_proxy(); |
| 81 | +} |
| 82 | + |
| 83 | +color_ostream::color_ostream() : ostream(new buffer(this)), cur_color(COLOR_RESET) |
| 84 | +{ |
| 85 | + // |
| 86 | +} |
| 87 | + |
| 88 | +color_ostream::~color_ostream() |
| 89 | +{ |
| 90 | + delete buf(); |
| 91 | +} |
| 92 | + |
| 93 | +void color_ostream::print(const char *format, ...) |
| 94 | +{ |
| 95 | + va_list args; |
| 96 | + va_start(args, format); |
| 97 | + vprint(format, args); |
| 98 | + va_end(args); |
| 99 | +} |
| 100 | + |
| 101 | +void color_ostream::vprint(const char *format, va_list args) |
| 102 | +{ |
| 103 | + std::string str = stl_vsprintf(format, args); |
| 104 | + |
| 105 | + if (!str.empty()) { |
| 106 | + flush_buffer(false); |
| 107 | + add_text(cur_color, str); |
| 108 | + if (str[str.size()-1] == '\n') |
| 109 | + flush_proxy(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void color_ostream::printerr(const char * format, ...) |
| 114 | +{ |
| 115 | + va_list args; |
| 116 | + va_start(args, format); |
| 117 | + vprinterr(format, args); |
| 118 | + va_end(args); |
| 119 | +} |
| 120 | + |
| 121 | +void color_ostream::vprinterr(const char *format, va_list args) |
| 122 | +{ |
| 123 | + color_value save = cur_color; |
| 124 | + |
| 125 | + fprintf(stderr, format, args); |
| 126 | + |
| 127 | + color(COLOR_LIGHTRED); |
| 128 | + vprint(format, args); |
| 129 | + color(save); |
| 130 | +} |
| 131 | + |
| 132 | +void color_ostream::color(color_value c) |
| 133 | +{ |
| 134 | + if (c == cur_color) |
| 135 | + return; |
| 136 | + |
| 137 | + flush_buffer(false); |
| 138 | + cur_color = c; |
| 139 | +} |
| 140 | + |
| 141 | +void color_ostream::reset_color(void) |
| 142 | +{ |
| 143 | + color(COLOR_RESET); |
| 144 | +} |
| 145 | + |
| 146 | +void buffered_color_ostream::add_text(color_value color, const std::string &text) |
| 147 | +{ |
| 148 | + if (text.empty()) |
| 149 | + return; |
| 150 | + |
| 151 | + if (buffer.empty()) |
| 152 | + { |
| 153 | + buffer.push_back(fragment_type(color, text)); |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + auto &back = buffer.back(); |
| 158 | + |
| 159 | + if (back.first != color || std::max(back.second.size(), text.size()) > 128) |
| 160 | + buffer.push_back(fragment_type(color, text)); |
| 161 | + else |
| 162 | + buffer.back().second += text; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +void color_ostream_proxy::flush_proxy() |
| 167 | +{ |
| 168 | + if (!buffer.empty()) |
| 169 | + { |
| 170 | + target->begin_batch(); |
| 171 | + |
| 172 | + for (auto it = buffer.begin(); it != buffer.end(); ++it) |
| 173 | + target->add_text(it->first, it->second); |
| 174 | + |
| 175 | + buffer.clear(); |
| 176 | + |
| 177 | + target->end_batch(); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +color_ostream_proxy::color_ostream_proxy(color_ostream &target) |
| 182 | + : target(&target) |
| 183 | +{ |
| 184 | + // |
| 185 | +} |
| 186 | + |
| 187 | +color_ostream_proxy::~color_ostream_proxy() |
| 188 | +{ |
| 189 | + *this << std::flush; |
| 190 | +} |
0 commit comments