log.cpp 8.3 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 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/* 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;

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) {
  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();
}

Log::~Log(void) {
  pthread_mutex_lock(&lock_);
  if (ofs_.is_open()) {
    ofs_.close();
  }
  pthread_mutex_unlock(&lock_);

  pthread_mutex_destroy(&lock_);
}

void Log::check_param_valid() {
  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;
}

bool Log::check_output(const LOG_LEVEL level, const char *module) {
  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;
}

int Log::output(const LOG_LEVEL level, const char *module, const char *prefix,
                const char *f, ...) {
  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;
}

int Log::set_console_level(LOG_LEVEL console_level) {
  if (LOG_LEVEL_PANIC <= console_level && console_level < LOG_LEVEL_LAST) {
    console_level_ = console_level;
    return LOG_STATUS_OK;
  }

  return LOG_STATUS_ERR;
}

LOG_LEVEL Log::get_console_level() { return console_level_; }

int Log::set_log_level(LOG_LEVEL log_level) {
  if (LOG_LEVEL_PANIC <= log_level && log_level < LOG_LEVEL_LAST) {
    log_level_ = log_level;
    return LOG_STATUS_OK;
  }

  return LOG_STATUS_ERR;
}

LOG_LEVEL Log::get_log_level() { return log_level_; }

const char *Log::prefix_msg(LOG_LEVEL level) {
  if (LOG_LEVEL_PANIC <= level && level < LOG_LEVEL_LAST) {
    return prefix_map_[level].c_str();
  }
  static const char *empty_prefix = "";
  return empty_prefix;
}

void Log::set_default_module(const std::string &modules) {
  split_string(modules, ",", default_set_);
}

int Log::set_rotate_type(LOG_ROTATE rotate_type) {
  if (LOG_ROTATE_BYDAY <= rotate_type && rotate_type < LOG_ROTATE_LAST) {
    rotate_type_ = rotate_type;
  }
  return LOG_STATUS_OK;
}

LOG_ROTATE Log::get_rotate_type() { return rotate_type_; }

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) {
    // 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;
}

int Log::rename_old_logs() {
  int log_index = 1;
  int max_log_index = 0;
  char log_index_str[4] = {0};

  while (log_index < 999) {
    snprintf(log_index_str, sizeof(log_index_str), "%03d", log_index);

    std::string log_name = log_name_ + "." + log_index_str;
    int result = access(log_name.c_str(), R_OK);
    if (result) {
      break;
    }

    max_log_index = log_index;
    log_index++;
  }

  log_index = max_log_index;
  while (log_index > 0) {
    snprintf(log_index_str, sizeof(log_index_str), "%03d", log_index);

    std::string log_name_old = log_name_ + "." + log_index_str;

    snprintf(log_index_str, sizeof(log_index_str), "%03d", log_index + 1);

    std::string log_name_new = log_name_ + "." + log_index_str;

    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;
}

int Log::rotate_by_size() {
  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) {
      std::cerr << "Failed to rename " << log_name_ << " to " << log_name_new
                << std::endl;
    }

    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;
}

int Log::rotate(const int year, const int month, const int day) {
  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;
}

LoggerFactory::LoggerFactory() {
  // Auto-generated constructor stub
}

LoggerFactory::~LoggerFactory() {
  // Auto-generated destructor stub
}

int LoggerFactory::init(const std::string &log_file, Log **logger,
                        LOG_LEVEL log_level, LOG_LEVEL console_level,
                        LOG_ROTATE rotate_type) {
  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;
}

int LoggerFactory::init_default(const std::string &log_file, LOG_LEVEL log_level,
                               LOG_LEVEL console_level, LOG_ROTATE rotate_type) {
  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);
}

} //namespace common