• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ios>
20 #include <sstream>
21 #include <thread>  // NOLINT (unapproved c++11 header)
22 
23 #if !defined(LIBGAV1_LOG_LEVEL)
24 #define LIBGAV1_LOG_LEVEL (1 << 30)
25 #endif
26 
27 namespace libgav1 {
28 namespace internal {
29 #if LIBGAV1_ENABLE_LOGGING
30 namespace {
31 
LogSeverityName(LogSeverity severity)32 const char* LogSeverityName(LogSeverity severity) {
33   switch (severity) {
34     case LogSeverity::kInfo:
35       return "INFO";
36     case LogSeverity::kError:
37       return "ERROR";
38     case LogSeverity::kWarning:
39       return "WARNING";
40   }
41   return "UNKNOWN";
42 }
43 
44 }  // namespace
45 
Log(LogSeverity severity,const char * file,int line,const char * format,...)46 void Log(LogSeverity severity, const char* file, int line, const char* format,
47          ...) {
48   if (LIBGAV1_LOG_LEVEL < static_cast<int>(severity)) return;
49   std::ostringstream ss;
50   ss << std::hex << std::this_thread::get_id();
51   fprintf(stderr, "%s %s %s:%d] ", LogSeverityName(severity), ss.str().c_str(),
52           file, line);
53 
54   va_list ap;
55   va_start(ap, format);
56   vfprintf(stderr, format, ap);
57   va_end(ap);
58   fprintf(stderr, "\n");
59 }
60 #else   // !LIBGAV1_ENABLE_LOGGING
61 void Log(LogSeverity /*severity*/, const char* /*file*/, int /*line*/,
62          const char* /*format*/, ...) {}
63 #endif  // LIBGAV1_ENABLE_LOGGING
64 
65 }  // namespace internal
66 }  // namespace libgav1
67