uvgVPCCenc 1.2.0
uvgVPCCenc is an open-source real-time V-PCC encoder library written in C++ from scratch.
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 * This file is part of uvgVPCCenc V-PCC encoder.
3 *
4 * Copyright (c) 2024-present, Tampere University, ITU/ISO/IEC, project contributors
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice, this
14 * list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * * Neither the name of Tampere University, ITU/ISO/IEC nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
32 ****************************************************************************/
33
34#pragma once
35
37
38#include <chrono>
39#include <cstdarg>
40#include <iomanip>
41#include <iostream>
42#include <mutex>
43#include <string>
44
45#ifndef UVG_LOG_LEVEL
46#define UVG_LOG_LEVEL LogLevel::DEBUG
47#endif
48
49namespace uvgutils {
50
53
54// stringify table for LogLevel
55static const std::string LogLevelStr[] = {"FATAL", "ERROR", "WARNING", "INFO", "PROFILING", "TRACE", "DEBUG"};
56
57constexpr bool errorsAreFatalDefaultValue = true;
59constexpr std::ostream* outputDefaultValue = &std::cerr;
60
61class Logger {
62 public:
63 static void setLogLevel(const LogLevel& level);
64 static void setErrorsAreFatal(const bool& isFatal);
65 static void setOutputStream(std::ostream& out);
66 static LogLevel getLogLevel();
67 static void printLogMessage(const std::string& context, LogLevel level, const std::string& message);
68
69 template <LogLevel LEVEL>
70 static void log(const std::string& context, const std::string& message) {
71 static std::mutex logMutex;
72 const std::lock_guard<std::mutex> lock(logMutex);
73 if constexpr (LEVEL <= COMPILETIME_LOG_LEVEL) {
74 if (LEVEL <= Logger::getLogLevel()) {
75 printLogMessage(context, LEVEL, message);
76 }
77 }
78 }
79 static std::string printfStrToStdStr(const char* fmt, ...);
80 static std::string vprintfStrToStdStr(const char* fmt, va_list args);
81
82 private:
83 static LogLevel logLevel;
84 static bool errorsAreFatal_;
85 static std::ostream* outputStream_;
86};
87
88template <LogLevel LEVEL>
89inline void log(const std::string& context, const std::string& message) {
90 Logger::log<LEVEL>(context, message);
91}
92
93class Timer {
94 public:
95 Timer() : start(std::chrono::steady_clock::now()) {}
96
97 // Function to get elapsed time since the start of the program
98 double elapsed() const {
99 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start).count() / 1000000.0;
100 }
101 std::string elapsed_str() const {
102 std::ostringstream oss;
103 oss << std::fixed << std::setprecision(6) << elapsed();
104 return oss.str();
105 }
106
107 private:
108 std::chrono::time_point<std::chrono::steady_clock> start;
109};
110
111static Timer global_timer;
112
113} // namespace uvgutils
Definition log.hpp:61
static void printLogMessage(const std::string &context, LogLevel level, const std::string &message)
Definition log.cpp:101
static void setOutputStream(std::ostream &out)
Definition log.cpp:98
static void setErrorsAreFatal(const bool &isFatal)
Definition log.cpp:97
static std::string vprintfStrToStdStr(const char *fmt, va_list args)
Definition log.cpp:134
static LogLevel getLogLevel()
Definition log.cpp:99
static void log(const std::string &context, const std::string &message)
Definition log.hpp:70
static std::string printfStrToStdStr(const char *fmt,...)
Definition log.cpp:120
static void setLogLevel(const LogLevel &level)
Definition log.cpp:96
Definition log.hpp:93
Timer()
Definition log.hpp:95
double elapsed() const
Definition log.hpp:98
std::string elapsed_str() const
Definition log.hpp:101
#define UVG_LOG_LEVEL
Definition log.hpp:46
Definition jobManagement.hpp:72
Definition jobManagement.hpp:50
constexpr LogLevel logLevelDefaultValue
Definition log.hpp:58
constexpr std::ostream * outputDefaultValue
Definition log.hpp:59
void log(const std::string &context, const std::string &message)
Definition log.hpp:89
constexpr bool errorsAreFatalDefaultValue
Definition log.hpp:57
constexpr LogLevel COMPILETIME_LOG_LEVEL
Definition log.hpp:52
LogLevel
Definition log.hpp:51