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