Debian Codegraph @

src/log.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2016, 2017 Computer Graphics Group, University of Siegen
5 * Written by Martin Lambers <[email protected]>
6 *
7 * Copyright (C) 2022
8 * Martin Lambers <[email protected]>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <cstdio>
30 #include <string>
31
32 #ifdef ANDROID
33 # include <android/log.h>
34 #endif
35
36 #include "log.hpp"
37
38 static LogLevel logLevel;
39 static std::string logFile;
40 static FILE* logStream = nullptr;
41
42 void SetLogLevel(LogLevel l)
43 {
44 logLevel = l;
45 }
46
47 LogLevel GetLogLevel()
48 {
49 return logLevel;
50 }
51
52 void SetLogFile(const char* name, bool truncate)
53 {
54 if (!name) {
55 logFile.clear();
56 logStream = nullptr;
57 } else {
58 if (truncate)
59 std::remove(name);
60 if (!(logStream = std::fopen(name, "a"))) {
61 LOG_WARNING("cannot open log file %s", name);
62 } else {
63 std::setbuf(logStream, nullptr);
64 logFile = name;
65 }
66 }
67 }
68
69 const char* GetLogFile()
70 {
71 return logFile.empty() ? nullptr : logFile.c_str();
72 }
73
74 void Log(LogLevel level, const char* s)
75 {
76 #ifdef ANDROID
77 // On Android, if there is no log file we always use the system log facility
78 // instead of stderr so that all messages are easily available in the Android monitor.
79 if (!logStream) {
80 int androidlogLevel = (
81 level == Log_Level_Fatal ? ANDROID_LOG_ERROR
82 : level == Log_Level_Warning ? ANDROID_LOG_WARN
83 : level == Log_Level_Info ? ANDROID_LOG_INFO
84 : level == Log_Level_Debug ? ANDROID_LOG_DEBUG
85 : ANDROID_LOG_VERBOSE);
86 __android_log_write(androidlogLevel, "Bino", s);
87 return;
88 }
89 #endif
90 (void)level;
91 // We want to print one complete line with exactly one call to fputs to
92 // line-buffered stderr so that the output of different processes is not
93 // mangled. Therefore we buffer what we want to print.
94 char buf[LOG_BUFSIZE] = "Bino";
95 int bufIndex = 4;
96 bufIndex += snprintf(buf + bufIndex, LOG_BUFSIZE - bufIndex, ": %s", s);
97 buf[std::min(bufIndex, LOG_BUFSIZE - 2)] = '\n';
98 bufIndex++;
99 buf[std::min(bufIndex, LOG_BUFSIZE - 1)] = '\0';
100 std::fputs(buf, logStream ? logStream : stderr);
101 }