Blender V5.0
log.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/log.h"
6#include "util/math.h"
7#include "util/string.h"
8#include "util/time.h"
9
10#include <cstdio>
11#ifdef _MSC_VER
12# define snprintf _snprintf
13#endif
14
16
19static double LOG_START_TIME = time_dt();
20
21const char *log_level_to_string(const LogLevel level)
22{
23 switch (level) {
24 case LOG_LEVEL_FATAL:
26 return "FATAL";
27 case LOG_LEVEL_ERROR:
29 return "ERROR";
32 return "WARNING";
34 case LOG_LEVEL_INFO:
35 return "INFO";
36 case LOG_LEVEL_DEBUG:
37 return "DEBUG";
38 case LOG_LEVEL_TRACE:
39 return "TRACE";
41 return "UNKNOWN";
42 }
43
44 return "";
45}
46
48{
49 const std::string str_lower = string_to_lower(str);
50
51 if (str_lower == "fatal") {
52 return LOG_LEVEL_FATAL;
53 }
54 if (str_lower == "error") {
55 return LOG_LEVEL_ERROR;
56 }
57 if (str_lower == "warning") {
58 return LOG_LEVEL_WARNING;
59 }
60 if (str_lower == "info") {
61 return LOG_LEVEL_INFO;
62 }
63 if (str_lower == "debug") {
64 return LOG_LEVEL_DEBUG;
65 }
66 if (str_lower == "trace") {
67 return LOG_LEVEL_TRACE;
68 }
69 return LOG_LEVEL_UNKNOWN;
70}
71
72void log_init(const LogFunction func)
73{
74 LOG_FUNCTION = func;
76}
77
78void log_level_set(const LogLevel level)
79{
80 LOG_LEVEL = level;
81}
82
83void log_level_set(const std::string &level)
84{
85 const LogLevel new_level = log_string_to_level(level);
86 if (new_level == LOG_LEVEL_UNKNOWN) {
87 LOG_ERROR << "Unknown log level specified: " << level;
88 return;
89 }
90 LOG_LEVEL = new_level;
91}
92
93static void log_default(const LogLevel level, const std::string &time_str, const char *msg)
94{
95 if (level >= LOG_LEVEL_INFO) {
96 printf("%s | %s\n", time_str.c_str(), msg);
97 }
98 else {
99 fflush(stdout);
100 fprintf(stderr, "%s | %s: %s\n", time_str.c_str(), log_level_to_string(level), msg);
101 }
102}
103
104void _log_message(const LogLevel level, const char *file_line, const char *func, const char *msg)
105{
106 assert(level <= LOG_LEVEL);
107
108 if (LOG_FUNCTION) {
109 LOG_FUNCTION(level, file_line, func, msg);
110 return;
111 }
112
113 const std::string time_str = time_human_readable_from_seconds(time_dt() - LOG_START_TIME);
114
115 if (strchr(msg, '\n') == nullptr) {
116 log_default(level, time_str, msg);
117 return;
118 }
119
120 vector<string> lines;
121 string_split(lines, msg, "\n", false);
122 for (const string &line : lines) {
123 log_default(level, time_str, line.c_str());
124 }
125
126 if (level == LOG_LEVEL_FATAL || level == LOG_LEVEL_DFATAL) {
127 abort();
128 }
129}
130
131std::ostream &operator<<(std::ostream &os, const int2 &value)
132{
133 os << "(" << value.x << ", " << value.y << ")";
134 return os;
135}
136
137std::ostream &operator<<(std::ostream &os, const float3 &value)
138{
139 os << "(" << value.x << ", " << value.y << ", " << value.z << ")";
140 return os;
141}
142
143std::ostream &operator<<(std::ostream &os, const float4 &value)
144{
145 os << "(" << value.x << ", " << value.y << ", " << value.z << ", " << value.w << ")";
146 return os;
147}
148
#define CCL_NAMESPACE_END
#define str(s)
#define assert(assertion)
#define printf(...)
void log_init(const LogFunction func)
Definition log.cpp:72
std::ostream & operator<<(std::ostream &os, const int2 &value)
Definition log.cpp:131
void _log_message(const LogLevel level, const char *file_line, const char *func, const char *msg)
Definition log.cpp:104
LogLevel log_string_to_level(const string &str)
Definition log.cpp:47
static double LOG_START_TIME
Definition log.cpp:19
static LogFunction LOG_FUNCTION
Definition log.cpp:18
CCL_NAMESPACE_BEGIN LogLevel LOG_LEVEL
Definition log.cpp:17
void log_level_set(const LogLevel level)
Definition log.cpp:78
static void log_default(const LogLevel level, const std::string &time_str, const char *msg)
Definition log.cpp:93
const char * log_level_to_string(const LogLevel level)
Definition log.cpp:21
LogLevel
Definition log.h:17
@ LOG_LEVEL_DERROR
Definition log.h:21
@ LOG_LEVEL_INFO_IMPORTANT
Definition log.h:24
@ LOG_LEVEL_DEBUG
Definition log.h:26
@ LOG_LEVEL_ERROR
Definition log.h:20
@ LOG_LEVEL_WARNING
Definition log.h:22
@ LOG_LEVEL_FATAL
Definition log.h:18
@ LOG_LEVEL_TRACE
Definition log.h:27
@ LOG_LEVEL_UNKNOWN
Definition log.h:28
@ LOG_LEVEL_DFATAL
Definition log.h:19
@ LOG_LEVEL_INFO
Definition log.h:25
@ LOG_LEVEL_DWARNING
Definition log.h:23
#define LOG_ERROR
Definition log.h:101
void(*)(const LogLevel level, const char *file_line, const char *func, const char *msg) LogFunction
Definition log.h:39
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:70
string string_to_lower(const string &s)
Definition string.cpp:210
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
string time_human_readable_from_seconds(const double seconds)
Definition time.cpp:143
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:47