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

file_logging.cpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e7a0131613bfca4169484ab267039db211324ff9 (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
#include "platform/file_logging.hpp"

#include "platform/platform.hpp"

#include "coding/file_writer.hpp"

#include <memory>
#include <mutex>
#include <sstream>

using namespace std;

namespace
{
  tm * GetLocalTime()
  {
    time_t rawTime;
    time(&rawTime);
    tm * localTime = localtime(&rawTime);
    assert(localTime);
    return localTime;
  }
}

void LogMessageFile(base::LogLevel level, base::SrcPoint const & srcPoint, string const & msg)
{
  static mutex mtx;
  static unique_ptr<FileWriter> file;

  string recordType;
  switch (level)
  {
  case LINFO: recordType.assign("INFO "); break;
  case LDEBUG: recordType.assign("DEBUG "); break;
  case LWARNING: recordType.assign("WARN "); break;
  case LERROR: recordType.assign("ERROR "); break;
  case LCRITICAL: recordType.assign("FATAL "); break;
  case NUM_LOG_LEVELS: CHECK(false, ()); break;
  }

  lock_guard<mutex> lock(mtx);

  if (file == nullptr)
  {
    if (GetPlatform().WritableDir().empty())
      return;
    tm * curTimeTM = GetLocalTime();
    stringstream fileName;
    fileName << "logging_" << curTimeTM->tm_year + 1900 << "_" << curTimeTM->tm_mon + 1 << "_" << curTimeTM->tm_mday << "_"
      << curTimeTM->tm_hour << "_" << curTimeTM->tm_min << "_" << curTimeTM->tm_sec << ".log";
    file.reset(new FileWriter(GetPlatform().WritablePathForFile(fileName.str())));
  }

  string srcString = recordType + DebugPrint(srcPoint) + " " + msg + "\n";

  file->Write(srcString.c_str(), srcString.size());
  file->Flush();
}

void LogMemoryInfo()
{
  static unsigned long counter = 0;
  const unsigned short writeLogEveryNthCall = 3;
  if (counter++ % writeLogEveryNthCall == 0)
  {
    tm * curTimeTM = GetLocalTime();
    stringstream timeDate;
    timeDate << " " << curTimeTM->tm_year + 1900 << "." << curTimeTM->tm_mon + 1 << "." << curTimeTM->tm_mday << " "
      << curTimeTM->tm_hour << ":" << curTimeTM->tm_min << ":" << curTimeTM->tm_sec << " ";
    LOG(LINFO, (timeDate.str(), GetPlatform().GetMemoryInfo()));
  }
}