Home | History | Annotate | Download | only in base
      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 #define LOG_TAG "perf_reader"
      8 
      9 #include "quipper/base/logging.h"
     10 
     11 #if defined(OS_POSIX)
     12 #include <errno.h>
     13 #include <pthread.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 #include <unistd.h>
     18 #endif
     19 
     20 #include <algorithm>
     21 #include <cstring>
     22 #include <ctime>
     23 #include <iomanip>
     24 #include <ostream>
     25 #include <string>
     26 
     27 #include <android/log.h>
     28 
     29 namespace logging {
     30 
     31 namespace {
     32 
     33 int min_log_level = 0;
     34 
     35 }
     36 
     37 void SetMinLogLevel(int level) {
     38   min_log_level = std::min(LOG_FATAL, level);
     39 }
     40 
     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 
     60 LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
     61     : severity_(severity), file_(file), line_(line) {
     62   Init(file, line);
     63 }
     64 
     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 
     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 
     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 
    107 void LogMessage::Init(const char* /* file */, int /* line */) {
    108 }
    109 
    110 }  // namespace logging
    111