MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LogHandler.cpp
1 /*
2  Copyright (C) 2003-2006, 2008 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "LogHandler.hpp"
20 
21 #include <NdbTick.h>
22 
23 //
24 // PUBLIC
25 //
27  m_pDateTimeFormat("%d-%.2d-%.2d %.2d:%.2d:%.2d"),
28  m_errorCode(0),
29  m_errorStr(NULL)
30 {
31  m_max_repeat_frequency= 3; // repeat messages maximum every 3 seconds
32  m_count_repeated_messages= 0;
33  m_last_category[0]= 0;
34  m_last_message[0]= 0;
35  m_last_log_time= 0;
36  m_now= 0;
37  m_last_level= (Logger::LoggerLevel)-1;
38 }
39 
41 {
42 }
43 
44 void
46  const char* pMsg)
47 {
48  time_t now;
49  now= ::time((time_t*)NULL);
50 
51  if (m_max_repeat_frequency == 0 ||
52  level != m_last_level ||
53  strcmp(pCategory, m_last_category) ||
54  strcmp(pMsg, m_last_message))
55  {
56  if (m_count_repeated_messages > 0) // print that message
57  append_impl(m_last_category, m_last_level, m_last_message);
58 
59  m_last_level= level;
60  strncpy(m_last_category, pCategory, sizeof(m_last_category));
61  strncpy(m_last_message, pMsg, sizeof(m_last_message));
62  }
63  else // repeated message
64  {
65  if (now < (time_t) (m_last_log_time+m_max_repeat_frequency))
66  {
67  m_count_repeated_messages++;
68  m_now= now;
69  return;
70  }
71  }
72 
73  m_now= now;
74 
75  append_impl(pCategory, level, pMsg);
76  m_last_log_time= now;
77 }
78 
79 void
80 LogHandler::append_impl(const char* pCategory, Logger::LoggerLevel level,
81  const char* pMsg)
82 {
83  writeHeader(pCategory, level);
84  if (m_count_repeated_messages <= 1)
85  writeMessage(pMsg);
86  else
87  {
88  BaseString str(pMsg);
89  str.appfmt(" - Repeated %d times", m_count_repeated_messages);
90  writeMessage(str.c_str());
91  }
92  m_count_repeated_messages= 0;
93  writeFooter();
94 }
95 
96 const char*
97 LogHandler::getDefaultHeader(char* pStr, const char* pCategory,
98  Logger::LoggerLevel level) const
99 {
100  char time[MAX_DATE_TIME_HEADER_LENGTH];
101  BaseString::snprintf(pStr, MAX_HEADER_LENGTH, "%s [%s] %s -- ",
102  getTimeAsString((char*)time),
103  pCategory,
104  Logger::LoggerLevelNames[level]);
105 
106  return pStr;
107 }
108 
109 
110 const char*
112 {
113  return "\n";
114 }
115 
116 const char*
118 {
119  return m_pDateTimeFormat;
120 }
121 
122 void
123 LogHandler::setDateTimeFormat(const char* pFormat)
124 {
125  m_pDateTimeFormat = (char*)pFormat;
126 }
127 
128 char*
129 LogHandler::getTimeAsString(char* pStr) const
130 {
131  struct tm* tm_now;
132  tm_now = ::localtime(&m_now); //uses the "current" timezone
133 
134  BaseString::snprintf(pStr, MAX_DATE_TIME_HEADER_LENGTH,
135  m_pDateTimeFormat,
136  tm_now->tm_year + 1900,
137  tm_now->tm_mon + 1, //month is [0,11]. +1 -> [1,12]
138  tm_now->tm_mday,
139  tm_now->tm_hour,
140  tm_now->tm_min,
141  tm_now->tm_sec);
142 
143  return pStr;
144 }
145 
146 int
148 {
149  return m_errorCode;
150 }
151 
152 void
154 {
155  m_errorCode = code;
156 }
157 
158 
159 char*
161 {
162  return m_errorStr;
163 }
164 
165 void
166 LogHandler::setErrorStr(const char* str)
167 {
168  m_errorStr= (char*) str;
169 }
170 
171 bool
173  Vector<BaseString> v_args;
174 
175  bool ret = true;
176 
177  _params.split(v_args, ",");
178  for(size_t i=0; i < v_args.size(); i++) {
179  Vector<BaseString> v_param_value;
180  if(v_args[i].split(v_param_value, "=", 2) != 2)
181  {
182  ret = false;
183  setErrorStr("Can't find key=value pair.");
184  }
185  else
186  {
187  v_param_value[0].trim(" \t");
188  if (!setParam(v_param_value[0], v_param_value[1]))
189  {
190  ret = false;
191  }
192  }
193  }
194 
195  if(!checkParams())
196  ret = false;
197  return ret;
198 }
199 
200 bool
202  return true;
203 }
204 
205 void LogHandler::setRepeatFrequency(unsigned val)
206 {
207  m_max_repeat_frequency= val;
208 }
209 
210 //
211 // PRIVATE
212 //