| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file log.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par Licence: |
| 9 | * Copyright (c) 2015-2026 Carsten Teibes |
| 10 | * |
| 11 | * OpenJazz is distributed under the terms of |
| 12 | * the GNU General Public License, version 2.0 |
| 13 | * |
| 14 | * @par Description: |
| 15 | * Logs to console and file. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "log.h"cxx . . $ `<file>/src/io/log.h`/ |
| 20 | #include <cstdarg> |
| 21 | #include <ctime> |
| 22 | #include <cstring> |
| 23 | #include <cstdlib> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #ifdef __vita__ |
| 27 | #include <psp2/kernel/clib.h> |
| 28 | #endif |
| 29 | |
| 30 | #ifdef __ANDROID__ |
| 31 | #include <android/log.h> |
| 32 | #endif |
| 33 | |
| 34 | static struct level_infocxx . . $ level_info# { |
| 35 | const char *namecxx . . $ level_info#name.; |
| 36 | const char *colorcxx . . $ level_info#color.; |
| 37 | } levelscxx . . $ levels.[7] = { |
| 38 | { "MAX", "\x1b[95m" }, |
| 39 | { "TRACE", "\x1b[94m" }, |
| 40 | { "DEBUG", "\x1b[36m" }, |
| 41 | { "INFO", "\x1b[32m" }, |
| 42 | { "WARN", "\x1b[33m" }, |
| 43 | { "ERROR", "\x1b[31m" }, |
| 44 | { "FATAL", "\x1b[35m" } |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Create logfile, set defaults |
| 49 | */ |
| 50 | Logcxx . . $ Log#::Logcxx . . $ Log#Log(49f6e7a06ebc5aa8). () { |
| 51 | |
| 52 | levelcxx . . $ Log#level. = LL_DEBUGcxx . . $ LL_DEBUG.; |
| 53 | quietcxx . . $ Log#quiet. = false; |
| 54 | logfilecxx . . $ Log#logfile. = fopen("openjazz.log", "w"); |
| 55 | color_stdoutcxx . . $ Log#color_stdout. = false; |
| 56 | color_stderrcxx . . $ Log#color_stderr. = false; |
| 57 | |
| 58 | // these systems may use colored terminal output |
| 59 | #if defined(__linux__cxx . . $ `<built-in>:449:9`!) || defined(__APPLE__) |
| 60 | // allow users to disable all colors (see https://no-color.org/) |
| 61 | if (!getenv("NO_COLOR")) { |
| 62 | color_stdoutcxx . . $ Log#color_stdout. = !!isatty(fileno(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!)); |
| 63 | color_stderrcxx . . $ Log#color_stderr. = !!isatty(fileno(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!)); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | #ifdef NDEBUG |
| 68 | // be more quiet in release builds |
| 69 | level = LL_INFO; |
| 70 | #endif |
| 71 | |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Close logfile |
| 76 | */ |
| 77 | Logcxx . . $ Log#::~cxx . . $ Log#`~Log`(49f6e7a06ebc5aa8).Logcxx . . $ Log# () { |
| 78 | |
| 79 | if (logfilecxx . . $ Log#logfile.) fclose(logfilecxx . . $ Log#logfile.); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set new loglevel. |
| 85 | * |
| 86 | * @param new_level The loglevel |
| 87 | */ |
| 88 | |
| 89 | void Logcxx . . $ Log#::setLevelcxx . . $ Log#setLevel(d4f767463ce0a6b3).(int new_level) { |
| 90 | |
| 91 | if (new_level > LL_FATALcxx . . $ LL_FATAL.) |
| 92 | levelcxx . . $ Log#level. = LL_FATALcxx . . $ LL_FATAL.; |
| 93 | else if (new_level < LL_MAXcxx . . $ LL_MAX.) |
| 94 | levelcxx . . $ Log#level. = LL_MAXcxx . . $ LL_MAX.; |
| 95 | else |
| 96 | levelcxx . . $ Log#level. = new_level; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the loglevel. |
| 102 | * |
| 103 | * @return The loglevel |
| 104 | */ |
| 105 | |
| 106 | int Logcxx . . $ Log#::getLevelcxx . . $ Log#getLevel(b126dc7c1de90089).() { |
| 107 | |
| 108 | return levelcxx . . $ Log#level.; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Enable/Disable quiet logging. |
| 114 | * |
| 115 | * @param enable Whether to enable quiet logging |
| 116 | */ |
| 117 | |
| 118 | void Logcxx . . $ Log#::setQuietcxx . . $ Log#setQuiet(b45ddcbc8423892c).(bool enable) { |
| 119 | |
| 120 | quietcxx . . $ Log#quiet. = enable; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Add a message to the log. |
| 126 | * |
| 127 | * @param lvl Verbosity level |
| 128 | * @param file Source file |
| 129 | * @param line Source line |
| 130 | * @param fmt printf formatted message |
| 131 | */ |
| 132 | |
| 133 | void Logcxx . . $ Log#::logcxx . . $ Log#log(cf10f40a72032897).(int lvl, const char *file, int line, const char *fmt, ...) { |
| 134 | #ifdef __ANDROID__ |
| 135 | // on android write only to journal, and do not spew for release builds |
| 136 | int maxlvl = LL_TRACE; //level; |
| 137 | #ifdef NDEBUG |
| 138 | maxlvl = LL_WARN; |
| 139 | #endif |
| 140 | |
| 141 | // log up to set verbosity |
| 142 | if(lvl < maxlvl) return; |
| 143 | |
| 144 | // use an immediate buffer for output |
| 145 | char outbuffer[1024]; |
| 146 | va_list args; |
| 147 | va_start(args, fmt); |
| 148 | vsnprintf(outbuffer, sizeof(outbuffer), fmt, args); |
| 149 | va_end(args); |
| 150 | |
| 151 | __android_log_write((lvl == LL_ERROR) ? ANDROID_LOG_ERROR : |
| 152 | (lvl == LL_WARN) ? ANDROID_LOG_WARN : |
| 153 | (lvl == LL_INFO) ? ANDROID_LOG_INFO : ANDROID_LOG_DEBUG, |
| 154 | "OpenJazz", outbuffer); |
| 155 | #else |
| 156 | // skip if nothing to write |
| 157 | if (!logfilecxx . . $ Log#logfile. && (lvl < levelcxx . . $ Log#level. || quietcxx . . $ Log#quiet.)) return; |
| 158 | |
| 159 | // extract file name (like basename) |
| 160 | const char *src = strrchr(file, '\\'); |
| 161 | if (!src) src = strrchr(file, '/'); |
| 162 | if (!src) |
| 163 | src = file; |
| 164 | else |
| 165 | src++; |
| 166 | |
| 167 | // get current time |
| 168 | time_tcxx . . $ time_t# t = time(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 169 | struct tmcxx . . $ tm# *now = localtime(&t); |
| 170 | |
| 171 | // log to console, up to set verbosity |
| 172 | if (!quietcxx . . $ Log#quiet. && lvl >= levelcxx . . $ Log#level.) { |
| 173 | // choose stderr/stdout depending on loglevel, use color if available |
| 174 | FILE *stream = stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!; |
| 175 | bool color = color_stdoutcxx . . $ Log#color_stdout.; |
| 176 | if (lvl < LL_INFOcxx . . $ LL_INFO.) { |
| 177 | stream = stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!; |
| 178 | color = color_stderrcxx . . $ Log#color_stderr.; |
| 179 | } |
| 180 | #ifdef __vita__ |
| 181 | // on vita we can only read stdout easily (with psp2shell) |
| 182 | #define LOG(...) sceClibPrintf(__VA_ARGS__) |
| 183 | #else |
| 184 | #define LOGcxx . . $ `src/io/log.cpp:184:11`!(...) fprintf(stream, __VA_ARGS__) |
| 185 | #endif |
| 186 | char timebuf[9]; |
| 187 | strftime(timebuf, 9, "%H:%M:%S", now); |
| 188 | |
| 189 | if (color) |
| 190 | LOGcxx . . $ `src/io/log.cpp:184:11`!("%s %s%-5s\x1b[0m ", timebuf, levels[lvl].color, levels[lvl].name); |
| 191 | else |
| 192 | LOGcxx . . $ `src/io/log.cpp:184:11`!("%s %-5s ", timebuf, levels[lvl].name); |
| 193 | |
| 194 | va_list args; |
| 195 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 196 | #ifdef __vita__ |
| 197 | // use an immediate buffer for output |
| 198 | char outbuffer[1024]; |
| 199 | vsnprintf(outbuffer, sizeof(outbuffer), fmt, args); |
| 200 | LOG(outbuffer); |
| 201 | #else |
| 202 | vfprintf(stream, fmt, args); |
| 203 | #endif |
| 204 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 205 | |
| 206 | // only include source information if doing debug logs |
| 207 | if (levelcxx . . $ Log#level. > LL_DEBUGcxx . . $ LL_DEBUG.) |
| 208 | LOGcxx . . $ `src/io/log.cpp:184:11`!("\n"); |
| 209 | else if (color) |
| 210 | LOGcxx . . $ `src/io/log.cpp:184:11`!(" \x1b[90m(%s:%d)\x1b[0m\n", src, line); |
| 211 | else |
| 212 | LOGcxx . . $ `src/io/log.cpp:184:11`!(" (%s:%d)\n", src, line); |
| 213 | |
| 214 | fflush(stream); |
| 215 | } |
| 216 | |
| 217 | // Log to file, do debug logs by default, higher if wanted |
| 218 | if (logfilecxx . . $ Log#logfile. && (lvl >= LL_DEBUGcxx . . $ LL_DEBUG. || levelcxx . . $ Log#level. <= lvl)) { |
| 219 | char timebuf[20]; |
| 220 | strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", now); |
| 221 | |
| 222 | fprintf(logfilecxx . . $ Log#logfile., "%s %-5s %s:%d: ", timebuf, levelscxx . . $ levels.[lvl].namecxx . . $ level_info#name., src, line); |
| 223 | |
| 224 | va_list args; |
| 225 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 226 | vfprintf(logfilecxx . . $ Log#logfile., fmt, args); |
| 227 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 228 | |
| 229 | fprintf(logfilecxx . . $ Log#logfile., "\n"); |
| 230 | |
| 231 | fflush(logfilecxx . . $ Log#logfile.); |
| 232 | } |
| 233 | #endif |
| 234 | } |