Home | History | Annotate | Download | only in lib
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "mojo/public/cpp/environment/lib/default_logger.h"
      6 
      7 #include <stdio.h>
      8 #include <stdlib.h>  // For |abort()|.
      9 
     10 #include <algorithm>
     11 
     12 #include "mojo/public/c/environment/logger.h"
     13 
     14 namespace mojo {
     15 
     16 namespace {
     17 
     18 MojoLogLevel g_minimum_log_level = MOJO_LOG_LEVEL_INFO;
     19 
     20 const char* GetLogLevelString(MojoLogLevel log_level) {
     21   if (log_level <= MOJO_LOG_LEVEL_VERBOSE-3)
     22     return "VERBOSE4+";
     23   switch (log_level) {
     24     case MOJO_LOG_LEVEL_VERBOSE-2:
     25       return "VERBOSE3";
     26     case MOJO_LOG_LEVEL_VERBOSE-1:
     27       return "VERBOSE2";
     28     case MOJO_LOG_LEVEL_VERBOSE:
     29       return "VERBOSE1";
     30     case MOJO_LOG_LEVEL_INFO:
     31       return "INFO";
     32     case MOJO_LOG_LEVEL_WARNING:
     33       return "WARNING";
     34     case MOJO_LOG_LEVEL_ERROR:
     35       return "ERROR";
     36   }
     37   // Consider everything higher to be fatal.
     38   return "FATAL";
     39 }
     40 
     41 void LogMessage(MojoLogLevel log_level, const char* message) {
     42   if (log_level < g_minimum_log_level)
     43     return;
     44 
     45   // TODO(vtl): Add timestamp also?
     46   fprintf(stderr, "%s: %s\n", GetLogLevelString(log_level), message);
     47   if (log_level >= MOJO_LOG_LEVEL_FATAL)
     48     abort();
     49 }
     50 
     51 MojoLogLevel GetMinimumLogLevel() {
     52   return g_minimum_log_level;
     53 }
     54 
     55 void SetMinimumLogLevel(MojoLogLevel minimum_log_level) {
     56   g_minimum_log_level = std::min(minimum_log_level, MOJO_LOG_LEVEL_FATAL);
     57 }
     58 
     59 }  // namespace
     60 
     61 namespace internal {
     62 
     63 const MojoLogger kDefaultLogger = {
     64   LogMessage,
     65   GetMinimumLogLevel,
     66   SetMinimumLogLevel
     67 };
     68 
     69 }  // namespace internal
     70 
     71 }  // namespace mojo
     72