Debian Codegraph @

src/log.hpp

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 /* This logging functionality is adapted from QVR */
30
31 #pragma once
32
33 #include <cstdio>
34
35 typedef enum {
36 /*! Print only fatal errors */
37 Log_Level_Fatal = 0,
38 /*! Additionally print warnings (default) */
39 Log_Level_Warning = 1,
40 /*! Additionally print informational messages */
41 Log_Level_Info = 2,
42 /*! Additionally print debugging information */
43 Log_Level_Debug = 3,
44 /*! Additionally print super verbose debugging information */
45 Log_Level_Firehose = 4
46 } LogLevel;
47
48 void SetLogLevel(LogLevel l);
49 LogLevel GetLogLevel();
50
51 void SetLogFile(const char* name, bool truncate); /* nullptr means stderr */
52 const char* GetLogFile(); /* nullptr means stderr */
53
54 /* Send one line to the log (\n will be appended) */
55 void Log(LogLevel level, const char* s);
56
57 #define LOG_BUFSIZE 1024
58
59 #define LOG_MSG(level, ...) { char buf[LOG_BUFSIZE]; snprintf(buf, LOG_BUFSIZE, __VA_ARGS__); Log(level, buf); }
60 #define LOG_REQUESTED(...) { LOG_MSG(Log_Level_Info, __VA_ARGS__); }
61 #define LOG_FATAL(...) { LOG_MSG(Log_Level_Fatal, __VA_ARGS__); }
62 #define LOG_WARNING(...) { if (GetLogLevel() >= Log_Level_Warning) { LOG_MSG(Log_Level_Warning, __VA_ARGS__); } }
63 #define LOG_INFO(...) { if (GetLogLevel() >= Log_Level_Info) { LOG_MSG(Log_Level_Info, __VA_ARGS__); } }
64 #define LOG_DEBUG(...) { if (GetLogLevel() >= Log_Level_Debug) { LOG_MSG(Log_Level_Debug, __VA_ARGS__); } }
65 #define LOG_FIREHOSE(...) { if (GetLogLevel() >= Log_Level_Firehose) { LOG_MSG(Log_Level_Firehose, __VA_ARGS__); } }