1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/utils/logging.h"
16
17 #include <cstdarg>
18 #include <cstdio>
19 #include <sstream>
20 #include <thread> // NOLINT (unapproved c++11 header)
21
22 #if !defined(LIBGAV1_LOG_LEVEL)
23 #define LIBGAV1_LOG_LEVEL (1 << 30)
24 #endif
25
26 namespace libgav1 {
27 namespace internal {
28 #if LIBGAV1_ENABLE_LOGGING
29 namespace {
30
LogSeverityName(LogSeverity severity)31 const char* LogSeverityName(LogSeverity severity) {
32 switch (severity) {
33 case LogSeverity::kInfo:
34 return "INFO";
35 case LogSeverity::kError:
36 return "ERROR";
37 case LogSeverity::kWarning:
38 return "WARNING";
39 }
40 return "UNKNOWN";
41 }
42
43 } // namespace
44
Log(LogSeverity severity,const char * file,int line,const char * format,...)45 void Log(LogSeverity severity, const char* file, int line, const char* format,
46 ...) {
47 if (LIBGAV1_LOG_LEVEL < static_cast<int>(severity)) return;
48 std::ostringstream ss;
49 ss << std::hex << std::this_thread::get_id();
50 fprintf(stderr, "%s %s %s:%d] ", LogSeverityName(severity), ss.str().c_str(),
51 file, line);
52
53 va_list ap;
54 va_start(ap, format);
55 vfprintf(stderr, format, ap);
56 va_end(ap);
57 fprintf(stderr, "\n");
58 }
59 #else // !LIBGAV1_ENABLE_LOGGING
60 void Log(LogSeverity /*severity*/, const char* /*file*/, int /*line*/,
61 const char* /*format*/, ...) {}
62 #endif // LIBGAV1_ENABLE_LOGGING
63
64 } // namespace internal
65 } // namespace libgav1
66