log.cpp 8.2 KB
Newer Older
羽飞's avatar
羽飞 已提交
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
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#include <assert.h>
#include <exception>
#include <stdarg.h>
#include <stdio.h>

#include "common/lang/string.h"
#include "common/log/log.h"
namespace common {

Log *g_log = nullptr;

26 27 28
Log::Log(const std::string &log_file_name, const LOG_LEVEL log_level, const LOG_LEVEL console_level)
    : log_name_(log_file_name), log_level_(log_level), console_level_(console_level)
{
羽飞's avatar
羽飞 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  prefix_map_[LOG_LEVEL_PANIC] = "PANIC:";
  prefix_map_[LOG_LEVEL_ERR] = "ERROR:";
  prefix_map_[LOG_LEVEL_WARN] = "WARNNING:";
  prefix_map_[LOG_LEVEL_INFO] = "INFO:";
  prefix_map_[LOG_LEVEL_DEBUG] = "DEBUG:";
  prefix_map_[LOG_LEVEL_TRACE] = "TRACE:";

  pthread_mutex_init(&lock_, nullptr);

  log_date_.year_ = -1;
  log_date_.mon_ = -1;
  log_date_.day_ = -1;
  log_max_line_ = LOG_MAX_LINE;
  log_line_ = -1;
  rotate_type_ = LOG_ROTATE_BYDAY;

  check_param_valid();
}

48 49
Log::~Log(void)
{
羽飞's avatar
羽飞 已提交
50 51 52 53 54 55 56 57 58
  pthread_mutex_lock(&lock_);
  if (ofs_.is_open()) {
    ofs_.close();
  }
  pthread_mutex_unlock(&lock_);

  pthread_mutex_destroy(&lock_);
}

59 60
void Log::check_param_valid()
{
羽飞's avatar
羽飞 已提交
61 62 63 64 65 66 67
  assert(!log_name_.empty());
  assert(LOG_LEVEL_PANIC <= log_level_ && log_level_ < LOG_LEVEL_LAST);
  assert(LOG_LEVEL_PANIC <= console_level_ && console_level_ < LOG_LEVEL_LAST);

  return;
}

68 69
bool Log::check_output(const LOG_LEVEL level, const char *module)
{
羽飞's avatar
羽飞 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
  if (LOG_LEVEL_LAST > level && level <= console_level_) {
    return true;
  }
  if (LOG_LEVEL_LAST > level && level <= log_level_) {
    return true;
  }
  // in order to improve speed
  if (default_set_.empty() == false && default_set_.find(module) != default_set_.end()) {
    return true;
  }
  return false;
}

83 84
int Log::output(const LOG_LEVEL level, const char *module, const char *prefix, const char *f, ...)
{
羽飞's avatar
羽飞 已提交
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
  bool locked = false;
  try {
    va_list args;
    char msg[ONE_KILO];

    va_start(args, f);
    vsnprintf(msg, sizeof(msg), f, args);
    va_end(args);

    if (LOG_LEVEL_PANIC <= level && level <= console_level_) {
      std::cout << msg << std::endl;
    } else if (default_set_.find(module) != default_set_.end()) {
      std::cout << msg << std::endl;
    }

    if (LOG_LEVEL_PANIC <= level && level <= log_level_) {
      pthread_mutex_lock(&lock_);
      locked = true;
      ofs_ << prefix;
      ofs_ << msg;
      ofs_ << "\n";
      ofs_.flush();
      log_line_++;
      pthread_mutex_unlock(&lock_);
      locked = false;
    } else if (default_set_.find(module) != default_set_.end()) {
      pthread_mutex_lock(&lock_);
      locked = true;
      ofs_ << prefix;
      ofs_ << msg;
      ofs_ << "\n";
      ofs_.flush();
      log_line_++;
      pthread_mutex_unlock(&lock_);
      locked = false;
    }

  } catch (std::exception &e) {
    if (locked) {
      pthread_mutex_unlock(&lock_);
    }
    std::cerr << e.what() << std::endl;
    return LOG_STATUS_ERR;
  }

  return LOG_STATUS_OK;
}

133 134
int Log::set_console_level(LOG_LEVEL console_level)
{
羽飞's avatar
羽飞 已提交
135 136 137 138 139 140 141 142
  if (LOG_LEVEL_PANIC <= console_level && console_level < LOG_LEVEL_LAST) {
    console_level_ = console_level;
    return LOG_STATUS_OK;
  }

  return LOG_STATUS_ERR;
}

143 144 145 146
LOG_LEVEL Log::get_console_level()
{
  return console_level_;
}
羽飞's avatar
羽飞 已提交
147

148 149
int Log::set_log_level(LOG_LEVEL log_level)
{
羽飞's avatar
羽飞 已提交
150 151 152 153 154 155 156 157
  if (LOG_LEVEL_PANIC <= log_level && log_level < LOG_LEVEL_LAST) {
    log_level_ = log_level;
    return LOG_STATUS_OK;
  }

  return LOG_STATUS_ERR;
}

158 159 160 161
LOG_LEVEL Log::get_log_level()
{
  return log_level_;
}
羽飞's avatar
羽飞 已提交
162

163 164
const char *Log::prefix_msg(LOG_LEVEL level)
{
羽飞's avatar
羽飞 已提交
165 166 167 168 169 170 171
  if (LOG_LEVEL_PANIC <= level && level < LOG_LEVEL_LAST) {
    return prefix_map_[level].c_str();
  }
  static const char *empty_prefix = "";
  return empty_prefix;
}

172 173
void Log::set_default_module(const std::string &modules)
{
羽飞's avatar
羽飞 已提交
174 175 176
  split_string(modules, ",", default_set_);
}

177 178
int Log::set_rotate_type(LOG_ROTATE rotate_type)
{
羽飞's avatar
羽飞 已提交
179 180 181 182 183 184
  if (LOG_ROTATE_BYDAY <= rotate_type && rotate_type < LOG_ROTATE_LAST) {
    rotate_type_ = rotate_type;
  }
  return LOG_STATUS_OK;
}

185 186 187 188
LOG_ROTATE Log::get_rotate_type()
{
  return rotate_type_;
}
羽飞's avatar
羽飞 已提交
189

190 191 192
int Log::rotate_by_day(const int year, const int month, const int day)
{
  if (log_date_.year_ == year && log_date_.mon_ == month && log_date_.day_ == day) {
羽飞's avatar
羽飞 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    // Don't need rotate
    return 0;
  }

  char date[16] = {0};
  snprintf(date, sizeof(date), "%04d%02d%02d", year, month, day);
  std::string log_file_name = log_name_ + "." + date;

  if (ofs_.is_open()) {
    ofs_.close();
  }
  ofs_.open(log_file_name.c_str(), std::ios_base::out | std::ios_base::app);
  if (ofs_.good()) {
    log_date_.year_ = year;
    log_date_.mon_ = month;
    log_date_.day_ = day;

    log_line_ = 0;
  }

  return 0;
}

L
Longda Feng 已提交
216 217
#define MAX_LOG_NUM 999

218 219
int Log::rename_old_logs()
{
羽飞's avatar
羽飞 已提交
220 221 222
  int log_index = 1;
  int max_log_index = 0;

L
Longda Feng 已提交
223 224
  while (log_index < MAX_LOG_NUM) {
    std::string log_name = log_name_ + "." + size_to_pad_str(log_index, 3);
羽飞's avatar
羽飞 已提交
225 226 227 228 229 230 231 232 233
    int result = access(log_name.c_str(), R_OK);
    if (result) {
      break;
    }

    max_log_index = log_index;
    log_index++;
  }

L
Longda Feng 已提交
234 235 236 237 238
  if (log_index == MAX_LOG_NUM) {
    std::string log_name_rm = log_name_ + "." + size_to_pad_str(log_index, 3);
    remove(log_name_rm.c_str());
  }

羽飞's avatar
羽飞 已提交
239 240
  log_index = max_log_index;
  while (log_index > 0) {
L
Longda Feng 已提交
241
    std::string log_name_old = log_name_ + "." + size_to_pad_str(log_index, 3);
羽飞's avatar
羽飞 已提交
242

L
Longda Feng 已提交
243
    std::string log_name_new = log_name_ + "." + size_to_pad_str(log_index + 1, 3);
羽飞's avatar
羽飞 已提交
244 245 246 247 248 249 250 251 252 253 254

    int result = rename(log_name_old.c_str(), log_name_new.c_str());
    if (result) {
      return LOG_STATUS_ERR;
    }
    log_index--;
  }

  return LOG_STATUS_OK;
}

255 256
int Log::rotate_by_size()
{
羽飞's avatar
羽飞 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  if (log_line_ < 0) {
    // The first time open log file
    ofs_.open(log_name_.c_str(), std::ios_base::out | std::ios_base::app);
    log_line_ = 0;
    return LOG_STATUS_OK;
  } else if (0 <= log_line_ && log_line_ < log_max_line_) {
    // Don't need rotate
    return LOG_STATUS_OK;
  } else {

    int result = rename_old_logs();
    if (result) {
      // rename old logs failed
      return LOG_STATUS_OK;
    }

    if (ofs_.is_open()) {
      ofs_.close();
    }

    char log_index_str[4] = {0};
    snprintf(log_index_str, sizeof(log_index_str), "%03d", 1);
    std::string log_name_new = log_name_ + "." + log_index_str;
    result = rename(log_name_.c_str(), log_name_new.c_str());
    if (result) {
282
      std::cerr << "Failed to rename " << log_name_ << " to " << log_name_new << std::endl;
羽飞's avatar
羽飞 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    }

    ofs_.open(log_name_.c_str(), std::ios_base::out | std::ios_base::app);
    if (ofs_.good()) {
      log_line_ = 0;
    } else {
      // Error
      log_line_ = log_max_line_;
    }

    return LOG_STATUS_OK;
  }

  return LOG_STATUS_OK;
}

299 300
int Log::rotate(const int year, const int month, const int day)
{
羽飞's avatar
羽飞 已提交
301 302 303 304 305 306 307 308 309 310 311 312
  int result = 0;
  pthread_mutex_lock(&lock_);
  if (rotate_type_ == LOG_ROTATE_BYDAY) {
    result = rotate_by_day(year, month, day);
  } else {
    result = rotate_by_size();
  }
  pthread_mutex_unlock(&lock_);

  return result;
}

313 314
LoggerFactory::LoggerFactory()
{
羽飞's avatar
羽飞 已提交
315 316 317
  // Auto-generated constructor stub
}

318 319
LoggerFactory::~LoggerFactory()
{
羽飞's avatar
羽飞 已提交
320 321 322
  // Auto-generated destructor stub
}

323 324 325
int LoggerFactory::init(
    const std::string &log_file, Log **logger, LOG_LEVEL log_level, LOG_LEVEL console_level, LOG_ROTATE rotate_type)
{
羽飞's avatar
羽飞 已提交
326 327 328 329 330 331 332 333 334 335 336 337
  Log *log = new (std::nothrow) Log(log_file, log_level, console_level);
  if (log == nullptr) {
    std::cout << "Error: fail to construct a log object!" << std::endl;
    return -1;
  }
  log->set_rotate_type(rotate_type);

  *logger = log;

  return 0;
}

338 339 340
int LoggerFactory::init_default(
    const std::string &log_file, LOG_LEVEL log_level, LOG_LEVEL console_level, LOG_ROTATE rotate_type)
{
羽飞's avatar
羽飞 已提交
341 342 343 344 345 346 347 348
  if (g_log != nullptr) {
    LOG_WARN("Default logger has been initialized");
    return 0;
  }

  return init(log_file, &g_log, log_level, console_level, rotate_type);
}

349
}  // namespace common