Welcome to mirror list, hosted at ThFree Co, Russian Federation.

logging.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fbf30b9539f4e9814bcaabc47721e753eed751a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "base/assert.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/timer.hpp"
#include "base/thread.hpp"
#include "base/mutex.hpp"

#include "std/iostream.hpp"
#include "std/iomanip.hpp"
#include "std/mutex.hpp"
#include "std/sstream.hpp"
#include "std/target_os.hpp"
#include "std/windows.hpp"


namespace my
{
  void LogCheckIfErrorLevel(LogLevel level)
  {
#ifdef DEBUG
    if (level >= LERROR)
#else
    if (level >= LCRITICAL)
#endif
    {
      CHECK(false, ("Error level is too serious", level));
    }
  }

#ifdef OMIM_OS_TIZEN
#include <FBaseLog.h>
  void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
  {
    ostringstream out;
    out << DebugPrint(srcPoint) << msg << endl;
    switch (level)
    {
    case LDEBUG:
      AppLogDebug(out.str().c_str());
      break;
    case LINFO:
    case LWARNING:
      AppLog(out.str().c_str());
      break;
    case LERROR:
    case LCRITICAL:
      AppLogException(out.str().c_str());
    }

  }
#else

  class LogHelper
  {
    int m_threadsCount;
    map<threads::ThreadID, int> m_threadID;

    int GetThreadID()
    {
      int & id = m_threadID[threads::GetCurrentThreadID()];
      if (id == 0)
        id = ++m_threadsCount;
      return id;
    }

    my::Timer m_timer;

    char const * m_names[5];
    size_t m_lens[5];

  public:
    threads::Mutex m_mutex;

    LogHelper() : m_threadsCount(0)
    {
      m_names[0] = "DEBUG"; m_lens[0] = 5;
      m_names[1] = "INFO"; m_lens[1] = 4;
      m_names[2] = "WARNING"; m_lens[2] = 7;
      m_names[3] = "ERROR"; m_lens[3] = 5;
      m_names[4] = "CRITICAL"; m_lens[4] = 8;
    }

    void WriteProlog(ostream & s, LogLevel level)
    {
      s << "LOG";

      s << " TID(" << GetThreadID() << ")";
      s << " " << m_names[level];

      double const sec = m_timer.ElapsedSeconds();
      s << " " << setfill(' ') << setw(static_cast<int>(16 - m_lens[level])) << sec << " ";
    }
  };

  void LogMessageDefault(LogLevel level, SrcPoint const & srcPoint, string const & msg)
  {
    static LogHelper logger;

    threads::MutexGuard guard(logger.m_mutex);
    UNUSED_VALUE(guard);

    ostringstream out;
    logger.WriteProlog(out, level);

    out << DebugPrint(srcPoint) << msg << endl;

    std::cerr << out.str();


  }
  void LogMessageTests(LogLevel level, SrcPoint const & srcPoint, string const & msg)
  {
    static mutex mtx;
    lock_guard<mutex> lock(mtx);

    ostringstream out;
    out << msg << endl;
    std::cerr << out.str();

#ifdef OMIM_OS_WINDOWS
    OutputDebugStringA(out.str().c_str());
#endif
    LogCheckIfErrorLevel(level);
  }

#endif

  LogMessageFn LogMessage = &LogMessageDefault;

  LogMessageFn SetLogMessageFn(LogMessageFn fn)
  {
    std::swap(LogMessage, fn);
    return fn;
  }

#ifdef DEBUG
  LogLevel g_LogLevel = LDEBUG;
#else
  LogLevel g_LogLevel = LINFO;
#endif
}