|
| 1 | +/********************************************************************************** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Antoine Beauchamp |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + *********************************************************************************/ |
| 24 | + |
| 25 | +#include "ConsoleLoggerService.h" |
| 26 | + |
| 27 | +#include "rapidassist/timing.h" |
| 28 | +#include "rapidassist/filesystem.h" |
| 29 | +#include "rapidassist/process.h" |
| 30 | + |
| 31 | +#include <Windows.h> // for GetThreadId |
| 32 | + |
| 33 | +#include <iostream> |
| 34 | + |
| 35 | +#if defined(_WIN32) |
| 36 | +#include <chrono> |
| 37 | +//typedef struct timeval |
| 38 | +//{ |
| 39 | +// long tv_sec; |
| 40 | +// long tv_usec; |
| 41 | +//} timeval; |
| 42 | +inline int gettimeofday(struct timeval* tp, struct timezone* tzp) |
| 43 | +{ |
| 44 | + namespace sc = std::chrono; |
| 45 | + sc::system_clock::duration d = sc::system_clock::now().time_since_epoch(); |
| 46 | + sc::seconds s = sc::duration_cast<sc::seconds>(d); |
| 47 | + tp->tv_sec = (long)s.count(); |
| 48 | + tp->tv_usec = (long)sc::duration_cast<sc::microseconds>(d - s).count(); |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | +#endif // _WIN32 |
| 53 | + |
| 54 | +namespace shellanything |
| 55 | +{ |
| 56 | + |
| 57 | + inline const char * GetLevelString(const ILoggerService::LOG_LEVEL& level) |
| 58 | + { |
| 59 | + switch (level) |
| 60 | + { |
| 61 | + default: |
| 62 | + case ILoggerService::LOG_LEVEL_DEBUG: |
| 63 | + return "DEBUG"; |
| 64 | + break; |
| 65 | + case ILoggerService::LOG_LEVEL_INFO: |
| 66 | + return "INFO"; |
| 67 | + break; |
| 68 | + case ILoggerService::LOG_LEVEL_WARNING: |
| 69 | + return "WARNING"; |
| 70 | + break; |
| 71 | + case ILoggerService::LOG_LEVEL_ERROR: |
| 72 | + return "ERROR"; |
| 73 | + break; |
| 74 | + case ILoggerService::LOG_LEVEL_FATAL: |
| 75 | + return "FATAL"; |
| 76 | + break; |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + inline std::string GetLogTimestamp() |
| 81 | + { |
| 82 | + struct timeval tv; |
| 83 | + time_t nowtime; |
| 84 | + struct tm nowtm; |
| 85 | + char tmbuf[64], buf[64]; |
| 86 | + |
| 87 | + gettimeofday(&tv, NULL); |
| 88 | + nowtime = tv.tv_sec; |
| 89 | + localtime_s(&nowtm, &nowtime); |
| 90 | + strftime(tmbuf, sizeof(tmbuf), "%H:%M:%S", &nowtm); // skip the date format string: %Y-%m-%d |
| 91 | + snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec); |
| 92 | + |
| 93 | + return buf; |
| 94 | + } |
| 95 | + |
| 96 | + ConsoleLoggerService::ConsoleLoggerService() |
| 97 | + { |
| 98 | + } |
| 99 | + |
| 100 | + ConsoleLoggerService::~ConsoleLoggerService() |
| 101 | + { |
| 102 | + } |
| 103 | + |
| 104 | + void ConsoleLoggerService::LogMessage(const char* filename, int line, const ILoggerService::LOG_LEVEL& level, const char* message) |
| 105 | + { |
| 106 | + /* |
| 107 | + W0928 09:06:07.668726 29440 ActionProperty.cpp:233] Reporting an error because fail is set to 'true'. |
| 108 | + E0928 09:06:14.720094 29440 ConfigManager.cpp:139] Failed searching for configuration files in directory 'D:\Projets\Programmation\Cpp\ShellAnything\ShellAnything\build\bin\Debug\test_workspace\TestPlugins.testProcess'. |
| 109 | + */ |
| 110 | + |
| 111 | + const char * level_str = GetLevelString(level); |
| 112 | + std::string log_time = GetLogTimestamp(); |
| 113 | + DWORD dwThreadId = GetThreadId(NULL); |
| 114 | + std::string actual_filename = ra::filesystem::GetFilename(filename); |
| 115 | + |
| 116 | + std::cout << level_str << " " << log_time << " " << dwThreadId << " " << actual_filename << ":" << line << "] " << message << "\n"; |
| 117 | + } |
| 118 | + |
| 119 | + void ConsoleLoggerService::LogMessage(const ILoggerService::LOG_LEVEL& level, const char* message) |
| 120 | + { |
| 121 | + LogMessage(__FILE__, __LINE__, level, message); |
| 122 | + } |
| 123 | + |
| 124 | +} //namespace shellanything |
0 commit comments