Subversion Repositories HomeAutomation

Rev

Rev 1961 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1961 Rev 1964
Line 29... Line 29...
29
 
29
 
30
namespace atom {
30
namespace atom {
31
namespace log {
31
namespace log {
32
 
32
 
33
static boost::mutex   mutex_;
33
static boost::mutex   mutex_;
-
 
34
static std::ofstream  file_;
34
static Level          level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO);
35
static Level          level_ = (Level)(LOG_LEVEL_ERROR | LOG_LEVEL_EXCEPTION | LOG_LEVEL_WARNING | LOG_LEVEL_INFO);
35
static const uint32_t kMaxBufferSize = 4096;
36
static const uint32_t kMaxBufferSize = 4096;
36
 
37
 
37
void SetLogLevel(Level level)
38
void SetLevel(Level level)
38
{
39
{
39
  level_ = level;
40
  level_ = level;
40
}
41
}
41
 
42
 
42
void SetLogLevelByString(std::string level_string)
43
void SetLevelByString(std::string level_string)
43
{
44
{
44
  level_ = LOG_LEVEL_NONE;
45
  level_ = LOG_LEVEL_NONE;
45
 
46
 
46
  if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos)
47
  if (level_string.find("LOG_LEVEL_ERROR") != std::string::npos)
47
  {
48
  {
Line 69... Line 70...
69
  }
70
  }
70
 
71
 
71
  if (level_string.find("LOG_LEVEL_ALL") != std::string::npos)
72
  if (level_string.find("LOG_LEVEL_ALL") != std::string::npos)
72
  {
73
  {
73
    level_ = (Level)(level_ | LOG_LEVEL_ALL);
74
    level_ = (Level)(level_ | LOG_LEVEL_ALL);
-
 
75
  }
-
 
76
}
-
 
77
 
-
 
78
bool OpenFile(std::string filepath)
-
 
79
{
-
 
80
  CloseFile();
-
 
81
 
-
 
82
  file_.open(filepath.data(), std::ios::app);
-
 
83
 
-
 
84
  return file_.is_open();
-
 
85
}
-
 
86
 
-
 
87
void CloseFile(void)
-
 
88
{
-
 
89
  if (file_.is_open())
-
 
90
  {
-
 
91
    file_.close();
74
  }
92
  }
75
}
93
}
76
 
94
 
77
void Print(Level level, std::string module, std::string message)
95
void Print(Level level, std::string module, std::string message)
78
{
96
{
-
 
97
  std::string               log_string;
79
  std::string               level_string;
98
  std::string               level_string;
80
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
99
  boost::posix_time::ptime  date_and_time(boost::posix_time::second_clock::local_time());
81
 
100
 
82
 
101
 
83
  /* Check log level */
102
  /* Check log level */
84
  if (0 == (level & level_))
103
  if (0 == (level & level_))
85
  {
104
  {
86
    return;
105
    return;
87
  }
106
  }
88
 
107
 
89
 
108
 
90
  /* Convert log level to string */
109
  /* Convert log level to string */
91
  if (0 != (level & LOG_LEVEL_ERROR))
110
  if (0 != (level & LOG_LEVEL_ERROR))
92
  {
111
  {
93
    level_string = "ERROR";
112
    level_string = "ERROR";
94
  }
113
  }
95
  else if (0 != (level & LOG_LEVEL_INFO))
114
  else if (0 != (level & LOG_LEVEL_INFO))
96
  {
115
  {
97
    level_string = "INFO";
116
    level_string = "INFO";
98
  }
117
  }
99
  else if (0 != (level & LOG_LEVEL_DEBUG))
118
  else if (0 != (level & LOG_LEVEL_DEBUG))
100
  {
119
  {
101
    level_string = "DEBUG";
120
    level_string = "DEBUG";
102
  }
121
  }
103
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
122
  else if (0 != (level & LOG_LEVEL_EXCEPTION))
104
  {
123
  {
105
    level_string = "EXCEPTION";
124
    level_string = "EXCEPTION";
106
  }
125
  }
107
  else if (0 != (level & LOG_LEVEL_WARNING))
126
  else if (0 != (level & LOG_LEVEL_WARNING))
108
  {
127
  {
109
    level_string = "WARNING";
128
    level_string = "WARNING";
110
  }
129
  }
111
  else
130
  else
112
  {
131
  {
113
    level_string = "UNKNOWN";
132
    level_string = "UNKNOWN";
114
  }
133
  }
115
 
134
 
116
 
135
 
117
  /* Adjust lengths of sub strings */
136
  /* Adjust lengths of sub strings */
118
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
137
  level_string.insert(level_string.end(), 9 - level_string.size(), ' ');
119
  module.insert(module.end(), 25 - module.size(), ' ');
138
  module.insert(module.end(), 25 - module.size(), ' ');
-
 
139
 
-
 
140
 
-
 
141
  /* Create log string */
-
 
142
  log_string = boost::posix_time::to_simple_string(date_and_time) + " " + level_string + " " + module + " " + message;
120
 
143
 
121
 
144
 
122
  /* Print message */
145
  /* Print message */
123
  mutex_.lock();
146
  mutex_.lock();
124
 
147
 
125
  std::cout << boost::posix_time::to_simple_string(date_and_time) << " " << level_string << " " << module << " " << message << std::endl;
148
  std::cout << log_string << std::endl;
-
 
149
 
-
 
150
 
-
 
151
  if (file_.is_open())
-
 
152
  {
-
 
153
    file_ << log_string << std::endl;
-
 
154
  }
-
 
155
 
126
  std::cout.imbue(std::locale::classic());
156
  std::cout.imbue(std::locale::classic());
127
 
157
 
128
  mutex_.unlock();
158
  mutex_.unlock();
129
}
159
}
130
 
160