Mana
Loading...
Searching...
No Matches
log.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2025 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "log.h"
23
24#include <SDL.h>
25
26#include <cstdarg>
27#include <fstream>
28#include <iostream>
29#include <sstream>
30#include <sys/time.h>
31
32static std::ofstream logFile;
33static bool logToStandardOut = true;
34
35static const char *getLogPriorityPrefix(SDL_LogPriority priority)
36{
37 switch (priority) {
38 case SDL_LOG_PRIORITY_WARN:
39 return "Warning: ";
40 case SDL_LOG_PRIORITY_ERROR:
41 return "Error: ";
42 case SDL_LOG_PRIORITY_CRITICAL:
43 return "Critical Error: ";
44 default:
45 return "";
46 }
47}
48
49static void logOutputFunction(void *userdata, int category, SDL_LogPriority priority, const char *message)
50{
51 // Get the current system time
52 timeval tv;
53 gettimeofday(&tv, nullptr);
54
55 // Create timestamp string
56 std::stringstream timeStr;
57 timeStr << "["
58 << ((((tv.tv_sec / 60) / 60) % 24 < 10) ? "0" : "")
59 << (int)(((tv.tv_sec / 60) / 60) % 24)
60 << ":"
61 << (((tv.tv_sec / 60) % 60 < 10) ? "0" : "")
62 << (int)((tv.tv_sec / 60) % 60)
63 << ":"
64 << ((tv.tv_sec % 60 < 10) ? "0" : "")
65 << (int)(tv.tv_sec % 60)
66 << "."
67 << (((tv.tv_usec / 10000) % 100) < 10 ? "0" : "")
68 << (int)((tv.tv_usec / 10000) % 100)
69 << "] ";
70
71 const char *prefix = getLogPriorityPrefix(priority);
72
73 if (logToStandardOut)
74 {
75 std::cout << timeStr.str() << prefix << message << std::endl;
76 }
77
78 if (logFile.is_open())
79 {
80 logFile << timeStr.str() << prefix << message << std::endl;
81 }
82}
83
85{
86 SDL_LogSetOutputFunction(logOutputFunction, nullptr);
87}
88
89void Log::setLogFile(const std::string &logFilename)
90{
91 logFile.open(logFilename, std::ios_base::trunc);
92
93 if (!logFile.is_open())
94 {
95 std::cout << "Warning: error while opening " << logFilename
96 << " for writing.\n";
97 }
98}
99
101{
102 logToStandardOut = value;
103}
104
105#define DEFINE_LOG_FUNCTION(name, priority) \
106 void Log::name(const char *fmt, ...) \
107 { \
108 va_list ap; \
109 va_start(ap, fmt); \
110 SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, priority, fmt, ap); \
111 va_end(ap); \
112 }
113
114DEFINE_LOG_FUNCTION(verbose, SDL_LOG_PRIORITY_VERBOSE)
115DEFINE_LOG_FUNCTION(debug, SDL_LOG_PRIORITY_DEBUG)
116DEFINE_LOG_FUNCTION(info, SDL_LOG_PRIORITY_INFO)
117DEFINE_LOG_FUNCTION(warn, SDL_LOG_PRIORITY_WARN)
118DEFINE_LOG_FUNCTION(error, SDL_LOG_PRIORITY_ERROR)
119
120#undef DEFINE_LOG_FUNCTION
121
122void Log::vinfo(const char *fmt, va_list ap)
123{
124 SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, fmt, ap);
125}
126
127void Log::critical(const std::string &message)
128{
129 SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", message.c_str());
130
131 if (!logToStandardOut)
132 {
133 std::cerr << getLogPriorityPrefix(SDL_LOG_PRIORITY_CRITICAL) << message << std::endl;
134 }
135
136 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message.c_str(), nullptr);
137 exit(1);
138}
#define DEFINE_LOG_FUNCTION(name, priority)
Definition log.cpp:105
void init()
Initializes the log system.
Definition log.cpp:84
void setLogToStandardOut(bool value)
Sets whether the log should be written to standard output.
Definition log.cpp:100
void setLogFile(const std::string &logFilename)
Sets the file to log to and opens it.
Definition log.cpp:89
void vinfo(const char *log_text, va_list ap)