1 //
2 // Logging support functions. These are designed to mimic those used in
3 // chromium_org/base in terms of interface, but to redirect error to
4 // the system log.
5 //
6 
7 #include "quipper/base/logging.h"
8 
9 #if defined(OS_POSIX)
10 #include <errno.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #endif
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <ctime>
21 #include <iomanip>
22 #include <ostream>
23 #include <string>
24 
25 #include <android/log.h>
26 
27 #define LOG_TAG "perf_reader"
28 
29 namespace logging {
30 
31 namespace {
32 
33 int min_log_level = 0;
34 
35 }
36 
SetMinLogLevel(int level)37 void SetMinLogLevel(int level) {
38   min_log_level = std::min(LOG_FATAL, level);
39 }
40 
GetMinLogLevel()41 int GetMinLogLevel() {
42   return min_log_level;
43 }
44 
45 // MSVC doesn't like complex extern templates and DLLs.
46 #if !defined(COMPILER_MSVC)
47 // Explicit instantiations for commonly used comparisons.
48 template std::string* MakeCheckOpString<int, int>(
49     const int&, const int&, const char* names);
50 template std::string* MakeCheckOpString<unsigned long, unsigned long>(
51     const unsigned long&, const unsigned long&, const char* names);
52 template std::string* MakeCheckOpString<unsigned long, unsigned int>(
53     const unsigned long&, const unsigned int&, const char* names);
54 template std::string* MakeCheckOpString<unsigned int, unsigned long>(
55     const unsigned int&, const unsigned long&, const char* names);
56 template std::string* MakeCheckOpString<std::string, std::string>(
57     const std::string&, const std::string&, const char* name);
58 #endif
59 
LogMessage(const char * file,int line,LogSeverity severity)60 LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
61     : severity_(severity), file_(file), line_(line) {
62   Init(file, line);
63 }
64 
LogMessage(const char * file,int line,std::string * result)65 LogMessage::LogMessage(const char* file, int line, std::string* result)
66     : severity_(LOG_FATAL), file_(file), line_(line) {
67   Init(file, line);
68   stream_ << "Check failed: " << *result;
69   delete result;
70 }
71 
LogMessage(const char * file,int line,LogSeverity severity,std::string * result)72 LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
73                        std::string* result)
74     : severity_(severity), file_(file), line_(line) {
75   Init(file, line);
76   stream_ << "Check failed: " << *result;
77   delete result;
78 }
79 
~LogMessage()80 LogMessage::~LogMessage() {
81   stream_ << std::endl;
82   std::string str_newline(stream_.str());
83 
84   android_LogPriority priority =
85       (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN;
86   switch (severity_) {
87     case LOG_INFO:
88       priority = ANDROID_LOG_INFO;
89       break;
90     case LOG_WARNING:
91       priority = ANDROID_LOG_WARN;
92       break;
93     case LOG_ERROR:
94       priority = ANDROID_LOG_ERROR;
95       break;
96     case LOG_FATAL:
97       priority = ANDROID_LOG_FATAL;
98       break;
99   }
100   __android_log_write(priority, LOG_TAG, str_newline.c_str());
101 
102   if (severity_ == LOG_FATAL) {
103     exit(9);
104   }
105 }
106 
Init(const char *,int)107 void LogMessage::Init(const char* /* file */, int /* line */) {
108 }
109 
110 }  // namespace logging
111