| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This program is free software; you can redistribute it and/or modify * |
| 9 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 10 |
* as published by the Free Software Foundation; either version 2 of * |
| 11 |
* the License, or (at your option) any later version. * |
| 12 |
* for detail see the LICENCE text file. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with FreeCAD; if not, write to the Free Software * |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 22 |
* USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
#include <FCConfig.h> |
| 27 |
|
| 28 |
#ifdef FC_OS_WIN32 |
| 29 |
# include <windows.h> |
| 30 |
#endif |
| 31 |
#include <cstring> |
| 32 |
#include <Python.h> |
| 33 |
|
| 34 |
#include <frameobject.h> |
| 35 |
|
| 36 |
#include "ConsoleObserver.h" |
| 37 |
#include "Interpreter.h" |
| 38 |
#include "Tools.h" |
| 39 |
|
| 40 |
|
| 41 |
using namespace Base; |
| 42 |
|
| 43 |
|
| 44 |
//========================================================================= |
| 45 |
// some special observers |
| 46 |
|
| 47 |
ConsoleObserverFile::ConsoleObserverFile(const char* sFileName) |
| 48 |
: cFileStream(Base::FileInfo(sFileName)) // can be in UTF8 |
| 49 |
{ |
| 50 |
if (!cFileStream.is_open()) { |
| 51 |
Console().warning("Cannot open log file '%s'.\n", sFileName); |
| 52 |
} |
| 53 |
// mark the file as a UTF-8 encoded file |
| 54 |
unsigned char bom[3] = {0xef, 0xbb, 0xbf}; |
| 55 |
cFileStream.write(reinterpret_cast<const char*>(bom), 3 * sizeof(char)); |
| 56 |
} |
| 57 |
|
| 58 |
ConsoleObserverFile::~ConsoleObserverFile() |
| 59 |
{ |
| 60 |
cFileStream.close(); |
| 61 |
} |
| 62 |
|
| 63 |
void ConsoleObserverFile::sendLog( |
| 64 |
const std::string& notifiername, |
| 65 |
const std::string& msg, |
| 66 |
LogStyle level, |
| 67 |
IntendedRecipient recipient, |
| 68 |
ContentType content |
| 69 |
) |
| 70 |
{ |
| 71 |
(void)notifiername; |
| 72 |
|
| 73 |
// Do not log translated messages, or messages intended only to the user to log file |
| 74 |
if (recipient == IntendedRecipient::User || content == ContentType::Translated) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
std::string prefix; |
| 79 |
switch (level) { |
| 80 |
case LogStyle::Warning: |
| 81 |
prefix = "Wrn: "; |
| 82 |
break; |
| 83 |
case LogStyle::Message: |
| 84 |
prefix = "Msg: "; |
| 85 |
break; |
| 86 |
case LogStyle::Error: |
| 87 |
prefix = "Err: "; |
| 88 |
break; |
| 89 |
case LogStyle::Log: |
| 90 |
prefix = "Log: "; |
| 91 |
break; |
| 92 |
case LogStyle::Critical: |
| 93 |
prefix = "Critical: "; |
| 94 |
break; |
| 95 |
default: |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
cFileStream << prefix << msg; |
| 100 |
cFileStream.flush(); |
| 101 |
} |
| 102 |
|
| 103 |
ConsoleObserverStd::ConsoleObserverStd() |
| 104 |
: |
| 105 |
#if defined(FC_OS_WIN32) |
| 106 |
useColorStderr(true) |
| 107 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 108 |
useColorStderr(isatty(STDERR_FILENO)) |
| 109 |
#else |
| 110 |
useColorStderr(false) |
| 111 |
#endif |
| 112 |
{ |
| 113 |
bLog = false; |
| 114 |
} |
| 115 |
|
| 116 |
ConsoleObserverStd::~ConsoleObserverStd() = default; |
| 117 |
|
| 118 |
void ConsoleObserverStd::sendLog( |
| 119 |
const std::string& notifiername, |
| 120 |
const std::string& msg, |
| 121 |
LogStyle level, |
| 122 |
IntendedRecipient recipient, |
| 123 |
ContentType content |
| 124 |
) |
| 125 |
{ |
| 126 |
(void)notifiername; |
| 127 |
|
| 128 |
// Do not log translated messages, or messages intended only to the user to std log |
| 129 |
if (recipient == IntendedRecipient::User || content == ContentType::Translated) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
switch (level) { |
| 134 |
case LogStyle::Warning: |
| 135 |
this->Warning(msg.c_str()); |
| 136 |
break; |
| 137 |
case LogStyle::Message: |
| 138 |
this->Message(msg.c_str()); |
| 139 |
break; |
| 140 |
case LogStyle::Error: |
| 141 |
this->Error(msg.c_str()); |
| 142 |
break; |
| 143 |
case LogStyle::Log: |
| 144 |
this->Log(msg.c_str()); |
| 145 |
break; |
| 146 |
case LogStyle::Critical: |
| 147 |
this->Critical(msg.c_str()); |
| 148 |
break; |
| 149 |
default: |
| 150 |
break; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void ConsoleObserverStd::Message(const char* sMsg) |
| 155 |
{ |
| 156 |
printf("%s", sMsg); |
| 157 |
} |
| 158 |
|
| 159 |
void ConsoleObserverStd::Warning(const char* sWarn) |
| 160 |
{ |
| 161 |
if (useColorStderr) { |
| 162 |
#if defined(FC_OS_WIN32) |
| 163 |
::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE); |
| 164 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 165 |
fprintf(stderr, "\033[1;33m"); |
| 166 |
#endif |
| 167 |
} |
| 168 |
|
| 169 |
fprintf(stderr, "%s", sWarn); |
| 170 |
|
| 171 |
if (useColorStderr) { |
| 172 |
#if defined(FC_OS_WIN32) |
| 173 |
::SetConsoleTextAttribute( |
| 174 |
::GetStdHandle(STD_ERROR_HANDLE), |
| 175 |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
| 176 |
); |
| 177 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 178 |
fprintf(stderr, "\033[0m"); |
| 179 |
#endif |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void ConsoleObserverStd::Error(const char* sErr) |
| 184 |
{ |
| 185 |
if (useColorStderr) { |
| 186 |
#if defined(FC_OS_WIN32) |
| 187 |
::SetConsoleTextAttribute( |
| 188 |
::GetStdHandle(STD_ERROR_HANDLE), |
| 189 |
FOREGROUND_RED | FOREGROUND_INTENSITY |
| 190 |
); |
| 191 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 192 |
fprintf(stderr, "\033[1;31m"); |
| 193 |
#endif |
| 194 |
} |
| 195 |
|
| 196 |
fprintf(stderr, "%s", sErr); |
| 197 |
|
| 198 |
if (useColorStderr) { |
| 199 |
#if defined(FC_OS_WIN32) |
| 200 |
::SetConsoleTextAttribute( |
| 201 |
::GetStdHandle(STD_ERROR_HANDLE), |
| 202 |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
| 203 |
); |
| 204 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 205 |
fprintf(stderr, "\033[0m"); |
| 206 |
#endif |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void ConsoleObserverStd::Log(const char* sLog) |
| 211 |
{ |
| 212 |
if (useColorStderr) { |
| 213 |
#if defined(FC_OS_WIN32) |
| 214 |
::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN); |
| 215 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 216 |
fprintf(stderr, "\033[1;36m"); |
| 217 |
#endif |
| 218 |
} |
| 219 |
|
| 220 |
fprintf(stderr, "%s", sLog); |
| 221 |
|
| 222 |
if (useColorStderr) { |
| 223 |
#if defined(FC_OS_WIN32) |
| 224 |
::SetConsoleTextAttribute( |
| 225 |
::GetStdHandle(STD_ERROR_HANDLE), |
| 226 |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
| 227 |
); |
| 228 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 229 |
fprintf(stderr, "\033[0m"); |
| 230 |
#endif |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void ConsoleObserverStd::Critical(const char* sCritical) |
| 235 |
{ |
| 236 |
if (useColorStderr) { |
| 237 |
#if defined(FC_OS_WIN32) |
| 238 |
::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE); |
| 239 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 240 |
fprintf(stderr, "\033[1;33m"); |
| 241 |
#endif |
| 242 |
} |
| 243 |
|
| 244 |
fprintf(stderr, "%s", sCritical); |
| 245 |
|
| 246 |
if (useColorStderr) { |
| 247 |
#if defined(FC_OS_WIN32) |
| 248 |
::SetConsoleTextAttribute( |
| 249 |
::GetStdHandle(STD_ERROR_HANDLE), |
| 250 |
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
| 251 |
); |
| 252 |
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD) |
| 253 |
fprintf(stderr, "\033[0m"); |
| 254 |
#endif |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
RedirectStdOutput::RedirectStdOutput() |
| 259 |
{ |
| 260 |
buffer.reserve(80); |
| 261 |
} |
| 262 |
|
| 263 |
int RedirectStdOutput::overflow(int ch) |
| 264 |
{ |
| 265 |
if (ch != EOF) { |
| 266 |
buffer.push_back(static_cast<char>(ch)); |
| 267 |
} |
| 268 |
return ch; |
| 269 |
} |
| 270 |
|
| 271 |
int RedirectStdOutput::sync() |
| 272 |
{ |
| 273 |
// Print as log as this might be verbose |
| 274 |
if (!buffer.empty() && buffer.back() == '\n') { |
| 275 |
Base::Console().log("%s", buffer.c_str()); |
| 276 |
buffer.clear(); |
| 277 |
} |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
RedirectStdLog::RedirectStdLog() |
| 282 |
{ |
| 283 |
buffer.reserve(80); |
| 284 |
} |
| 285 |
|
| 286 |
int RedirectStdLog::overflow(int ch) |
| 287 |
{ |
| 288 |
if (ch != EOF) { |
| 289 |
buffer.push_back(static_cast<char>(ch)); |
| 290 |
} |
| 291 |
return ch; |
| 292 |
} |
| 293 |
|
| 294 |
int RedirectStdLog::sync() |
| 295 |
{ |
| 296 |
// Print as log as this might be verbose |
| 297 |
if (!buffer.empty() && buffer.back() == '\n') { |
| 298 |
Base::Console().log("%s", buffer.c_str()); |
| 299 |
buffer.clear(); |
| 300 |
} |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
RedirectStdError::RedirectStdError() |
| 305 |
{ |
| 306 |
buffer.reserve(80); |
| 307 |
} |
| 308 |
|
| 309 |
int RedirectStdError::overflow(int ch) |
| 310 |
{ |
| 311 |
if (ch != EOF) { |
| 312 |
buffer.push_back(static_cast<char>(ch)); |
| 313 |
} |
| 314 |
return ch; |
| 315 |
} |
| 316 |
|
| 317 |
int RedirectStdError::sync() |
| 318 |
{ |
| 319 |
if (!buffer.empty() && buffer.back() == '\n') { |
| 320 |
Base::Console().error("%s", buffer.c_str()); |
| 321 |
buffer.clear(); |
| 322 |
} |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
//--------------------------------------------------------- |
| 327 |
|
| 328 |
std::stringstream& LogLevel::prefix(std::stringstream& str, const char* src, int line) |
| 329 |
{ |
| 330 |
static FC_TIME_POINT s_tstart; |
| 331 |
static bool s_timing = false; |
| 332 |
if (print_time) { |
| 333 |
if (!s_timing) { |
| 334 |
s_timing = true; |
| 335 |
_FC_TIME_INIT(s_tstart); |
| 336 |
} |
| 337 |
auto tnow = std::chrono::FC_TIME_CLOCK::now(); |
| 338 |
auto dc = std::chrono::duration_cast<FC_DURATION>(tnow - s_tstart); |
| 339 |
str << dc.count() << ' '; |
| 340 |
} |
| 341 |
if (print_tag) { |
| 342 |
str << '<' << tag << "> "; |
| 343 |
} |
| 344 |
if (print_src == 2) { |
| 345 |
Base::PyGILStateLocker lock; |
| 346 |
PyFrameObject* frame = PyEval_GetFrame(); |
| 347 |
if (frame) { |
| 348 |
line = PyFrame_GetLineNumber(frame); |
| 349 |
#if PY_VERSION_HEX < 0x030b0000 |
| 350 |
src = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 351 |
#else |
| 352 |
PyCodeObject* code = PyFrame_GetCode(frame); |
| 353 |
src = PyUnicode_AsUTF8(code->co_filename); |
| 354 |
Py_DECREF(code); |
| 355 |
#endif |
| 356 |
} |
| 357 |
} |
| 358 |
if (print_src && !Base::Tools::isNullOrEmpty(src)) { |
| 359 |
#ifdef FC_OS_WIN32 |
| 360 |
const char* _f = std::strrchr(src, '\\'); |
| 361 |
#else |
| 362 |
const char* _f = std::strrchr(src, '/'); |
| 363 |
#endif |
| 364 |
str << (_f ? _f + 1 : src) << "(" << line << "): "; |
| 365 |
} |
| 366 |
return str; |
| 367 |
} |