void PrintTraceLine(const char* function, const char* source_file, const int source_line, const TRACE_ERRORLEVEL level, const char* format, ...) { va_list vl; va_start(vl, format); static char timestamp[__MAX_TRACE_TIMESTAMP_LENGTH]; if (!_trace_initialized) { InitializeTrace(); } if (level <= _trace_level) { time_t t; struct tm *tmp; time(&t); tmp = localtime(&t); strftime(timestamp, sizeof(timestamp), "%Y.%m.%d %H:%M:%S", tmp); fprintf(_trace_file, "[%s] %s ", timestamp, GetLevelString(level)); fprintf(_trace_file, "<%s> (%s:%d) > ", function, source_file, source_line); vfprintf(_trace_file, format, vl); fprintf(_trace_file, "\n"); fflush(_trace_file); } }
string LogMessage(LogLevel level, string message) { auto now = std::chrono::system_clock::now(); auto in_time_t = std::chrono::system_clock::to_time_t(now); cout << std::put_time(std::localtime(&in_time_t), "%H:%M:%S:") << GetLevelString(level) << ": " << message << endl; return message; }
void LogAppend(ELogLevel aLogLevel, const char* aFile, const std::string& aFunc, const std::string& aMessage, int aLine, ...) { if (!loggerInitialized && !uninitializedLoggerUseReported) { uninitializedLoggerUseReported = true; std::cout << "WARNING: Logger used without initialization. Call InitLogFile() first!" << std::endl; } // Remove the path from the filename aFile = RemovePath(aFile); // Assemble the user-generated message char buffer[510]; va_list args; va_start(args, aLine); #ifdef AK_SYSTEM_WINDOWS vsnprintf_s(buffer, 500, _TRUNCATE, aMessage.c_str(), args); #else vsnprintf(buffer, 500, aMessage.c_str(), args); #endif va_end(args); CClock now; std::string timestamp(now.GetTimeLong()); if (loggerInitialized && aLogLevel >= fileLogLevel) { WriteLogEntry(*logFile, timestamp, GetLevelString(aLogLevel), aFile, aFunc, aLine, buffer); } if (aLogLevel >= consoleLogLevel) { #ifdef AK_SYSTEM_ANDROID __android_log_print(ANDROID_LOG_DEBUG + static_cast<int32_t>(aLogLevel), appName.c_str(), buffer); #else if (simplifiedConsoleOutput) { std::cout << buffer << std::endl; } else { WriteLogEntry(std::cout, timestamp, GetLevelString(aLogLevel), aFile, aFunc, aLine, buffer); } #endif } }
void CLog::Log(LOG_LEVEL level, const char *file, int line, int error, char *fmt, ...) { if(m_pLog == NULL) return; // no log file specified if(level > m_level) return; // too detail // format time char strtime[128]; time_t t = time(NULL); struct tm *tt = gmtime(&t); snprintf(strtime, sizeof(strtime)-1, "%d-%d-%d %d:%d:%d", 1900+tt->tm_year, tt->tm_mon, tt->tm_yday, tt->tm_hour, tt->tm_min, tt->tm_sec); // format message char buf[64*1024]; // max error message size in Windows va_list maker; va_start(maker, fmt); vsnprintf(buf, sizeof(buf)-1, fmt, maker); va_end(maker); fprintf(m_pLog, "[%s][%s][%s][line:%d]%s[errno=%d]", strtime, GetLevelString(level), file, line, buf, error); // format error strings buf[0] = '\0'; if(error != 0) { #ifdef WIN32 // Windows FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, DWORD(error), 0, buf, sizeof(buf)-1, NULL); fprintf(m_pLog, "%s.\n", buf); #else // Linux fprintf(m_pLog, "%s.\n", strerror(error)); #endif } else { fprintf(m_pLog, ".\n"); } // flush file for error message if(level == LL_ERROR) { fflush(m_pLog); } }
std::string Log::GetPrefix(LogLevel level) { return GetLevelColor(level) + GetLevelString(level) + ": "; }
static void InitializeTrace() { int trace_level_defined; int trace_file_defined; if (!getenv(__TRACE_TRACE_LEVEL_ENV)) { _trace_level = __TRACE_DEFAULTLEVEL; trace_level_defined=__TRACE_NO_LEVEL_DEFINED; } else if (!strcmp(getenv(__TRACE_TRACE_LEVEL_ENV), __TRACE_LEVEL_OFF)) { _trace_level = TRACE_OFF; trace_level_defined=__TRACE_LEVEL_DEFINED; } else if (!strcmp(getenv(__TRACE_TRACE_LEVEL_ENV), __TRACE_LEVEL_ERROR)) { _trace_level = TRACE_ERROR; trace_level_defined=__TRACE_LEVEL_DEFINED; } else if (!strcmp(getenv(__TRACE_TRACE_LEVEL_ENV), __TRACE_LEVEL_WARNING)) { _trace_level = TRACE_WARNING; trace_level_defined=__TRACE_LEVEL_DEFINED; } else if (!strcmp(getenv(__TRACE_TRACE_LEVEL_ENV), __TRACE_LEVEL_INFO)) { _trace_level = TRACE_INFO; trace_level_defined=__TRACE_LEVEL_DEFINED; } else if (!strcmp(getenv(__TRACE_TRACE_LEVEL_ENV), __TRACE_LEVEL_DEBUG)) { _trace_level = TRACE_DEBUG; trace_level_defined=__TRACE_LEVEL_DEFINED; } else { _trace_level = __TRACE_DEFAULTLEVEL; trace_level_defined=__TRACE_WRONG_LEVEL_DEFINED; } if (!getenv(__TRACE_TRACE_FILENAME)) { _trace_file = stdout; trace_file_defined=__TRACE_NO_FILE_DEFINED; } else if (!(_trace_file=fopen(getenv(__TRACE_TRACE_FILENAME), "a"))) { _trace_file = stdout; trace_file_defined=__TRACE_FINE_NOT_OPEN; } else { trace_file_defined=__TRACE_FILE_DEFINED; } _trace_initialized=1; switch(trace_level_defined) { case __TRACE_NO_LEVEL_DEFINED: TRACE(TRACE_ERROR, "No trace level selected, defaulting to %s", GetLevelString(__TRACE_DEFAULTLEVEL)); break; case __TRACE_LEVEL_DEFINED: TRACE(TRACE_INFO, "Trace level: %s", GetLevelString(_trace_level)); break; case __TRACE_WRONG_LEVEL_DEFINED: TRACE(TRACE_ERROR, "Wrong trace level selected: %s, defaulting to %s", getenv(__TRACE_TRACE_LEVEL_ENV), GetLevelString(__TRACE_DEFAULTLEVEL)); break; } switch(trace_level_defined) { case __TRACE_NO_FILE_DEFINED: TRACE(TRACE_WARNING, "No trace file specified, outputting to standard output"); break; case __TRACE_FINE_NOT_OPEN: TRACE(TRACE_ERROR, "Unable to open trace file, outputting to standard output"); break; } }