uvgVPCCenc 1.1.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 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 the Tampere University or 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 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
28 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ****************************************************************************/
32
33#pragma once
34
36
37#include <chrono>
38#include <cstdarg>
39#include <iomanip>
40#include <iostream>
41#include <string>
42#include <mutex>
43
44#ifndef UVG_LOG_LEVEL
45#define UVG_LOG_LEVEL LogLevel::DEBUG
46#endif
47
48namespace uvgvpcc_enc {
49
52
53// stringify table for LogLevel
54static const std::string LogLevelStr[] = {"FATAL", "ERROR", "WARNING", "INFO", "PROFILING", "TRACE", "DEBUG"};
55
56constexpr bool errorsAreFatalDefaultValue = true;
58constexpr std::ostream* outputDefaultValue = &std::cerr;
59
60class Logger {
61 public:
62 static void setLogLevel(const LogLevel& level);
63 static void setErrorsAreFatal(const bool& isFatal);
64 static void setOutputStream(std::ostream& out);
65 static LogLevel getLogLevel();
66 static void printLogMessage(const std::string& context, LogLevel level, const std::string& message);
67
68 template <LogLevel LEVEL>
69 static void log(const std::string& context, const std::string& message) {
70 static std::mutex logMutex;
71 const std::lock_guard<std::mutex> lock(logMutex);
72 if constexpr (LEVEL <= COMPILETIME_LOG_LEVEL) {
73 if (LEVEL <= Logger::getLogLevel()) {
74 printLogMessage(context, LEVEL, message);
75 }
76 }
77 }
78 static std::string printfStrToStdStr(const char* fmt, ...);
79 static std::string vprintfStrToStdStr(const char* fmt, va_list args);
80
81 private:
82 static LogLevel logLevel;
83 static bool errorsAreFatal_;
84 static std::ostream* outputStream_;
85
86
87};
88
89class Timer {
90 public:
91 Timer() : start(std::chrono::steady_clock::now()) {}
92
93 // Function to get elapsed time since the start of the program
94 double elapsed() const {
95 return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start).count() / 1000000.0;
96 }
97 std::string elapsed_str() const {
98 std::ostringstream oss;
99 oss << std::fixed << std::setprecision(6) << elapsed();
100 return oss.str();
101 }
102
103 private:
104 std::chrono::time_point<std::chrono::steady_clock> start;
105};
106
107static Timer global_timer;
108
109} // namespace uvgvpcc_enc
Definition log.hpp:60
static void log(const std::string &context, const std::string &message)
Definition log.hpp:69
static LogLevel getLogLevel()
Definition log.cpp:98
static void setOutputStream(std::ostream &out)
Definition log.cpp:97
static void setErrorsAreFatal(const bool &isFatal)
Definition log.cpp:96
static std::string vprintfStrToStdStr(const char *fmt, va_list args)
Definition log.cpp:133
static std::string printfStrToStdStr(const char *fmt,...)
Definition log.cpp:119
static void printLogMessage(const std::string &context, LogLevel level, const std::string &message)
Definition log.cpp:100
static void setLogLevel(const LogLevel &level)
Definition log.cpp:95
Definition log.hpp:89
std::string elapsed_str() const
Definition log.hpp:97
double elapsed() const
Definition log.hpp:94
Timer()
Definition log.hpp:91
#define UVG_LOG_LEVEL
Definition log.hpp:45
Definition jobManagement.cpp:168
Definition log.hpp:48
constexpr LogLevel logLevelDefaultValue
Definition log.hpp:57
constexpr LogLevel COMPILETIME_LOG_LEVEL
Definition log.hpp:51
constexpr bool errorsAreFatalDefaultValue
Definition log.hpp:56
constexpr std::ostream * outputDefaultValue
Definition log.hpp:58
LogLevel
Definition log.hpp:50