32static std::ofstream logFile;
33static bool logToStandardOut =
true;
35static const char *getLogPriorityPrefix(SDL_LogPriority priority)
38 case SDL_LOG_PRIORITY_WARN:
40 case SDL_LOG_PRIORITY_ERROR:
42 case SDL_LOG_PRIORITY_CRITICAL:
43 return "Critical Error: ";
49static void logOutputFunction(
void *userdata,
int category, SDL_LogPriority priority,
const char *message)
53 gettimeofday(&tv,
nullptr);
56 std::stringstream timeStr;
58 << ((((tv.tv_sec / 60) / 60) % 24 < 10) ?
"0" :
"")
59 << (int)(((tv.tv_sec / 60) / 60) % 24)
61 << (((tv.tv_sec / 60) % 60 < 10) ?
"0" :
"")
62 << (int)((tv.tv_sec / 60) % 60)
64 << ((tv.tv_sec % 60 < 10) ?
"0" :
"")
65 << (int)(tv.tv_sec % 60)
67 << (((tv.tv_usec / 10000) % 100) < 10 ?
"0" :
"")
68 << (int)((tv.tv_usec / 10000) % 100)
71 const char *prefix = getLogPriorityPrefix(priority);
75 std::cout << timeStr.str() << prefix << message << std::endl;
78 if (logFile.is_open())
80 logFile << timeStr.str() << prefix << message << std::endl;
86 SDL_LogSetOutputFunction(logOutputFunction,
nullptr);
91 logFile.open(logFilename, std::ios_base::trunc);
93 if (!logFile.is_open())
95 std::cout <<
"Warning: error while opening " << logFilename
102 logToStandardOut = value;
105#define DEFINE_LOG_FUNCTION(name, priority) \
106 void Log::name(const char *fmt, ...) \
110 SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, priority, fmt, ap); \
120#undef DEFINE_LOG_FUNCTION
124 SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
127void Log::critical(
const std::string &message)
129 SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION,
"%s", message.c_str());
131 if (!logToStandardOut)
133 std::cerr << getLogPriorityPrefix(SDL_LOG_PRIORITY_CRITICAL) << message << std::endl;
136 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Error", message.c_str(),
nullptr);
#define DEFINE_LOG_FUNCTION(name, priority)
void init()
Initializes the log system.
void setLogToStandardOut(bool value)
Sets whether the log should be written to standard output.
void setLogFile(const std::string &logFilename)
Sets the file to log to and opens it.
void vinfo(const char *log_text, va_list ap)