logging: debug.h: use DoutStreambuf

Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
This commit is contained in:
Colin Patrick McCabe 2010-12-07 23:53:12 -08:00
parent c53ffafb5c
commit 986c2af4ae
5 changed files with 52 additions and 203 deletions

View File

@ -75,7 +75,7 @@ static void primitive_log(const std::string &str)
static inline bool prio_is_visible_on_stderr(int prio)
{
return prio <= 15;
return prio <= 9;
}
static inline int dout_prio_to_syslog_prio(int prio)
@ -140,7 +140,6 @@ static int create_symlink(string oldpath, const string &newpath)
// Create relative symlink if the files are in the same directory
if (get_dirname(oldpath) == get_dirname(newpath)) {
oldpath = string("./") + get_basename(oldpath);
cerr << "setting oldpath = '" << oldpath << "'" << std::endl;
}
while (1) {

View File

@ -44,13 +44,6 @@ void common_init(std::vector<const char*>& args, const char *module_type, bool i
}
#endif //HAVE_LIBTCMALLOC
if (g_conf.log_file && g_conf.log_file[0])
g_conf.log_to_stdout = false;
// open log file?
if (!g_conf.log_to_stdout)
_dout_open_log();
if (init_keys && is_supported_auth(CEPH_AUTH_CEPHX)) {
g_keyring.load(g_conf.keyring);

View File

@ -1,210 +1,55 @@
#include "include/types.h"
#include "Mutex.h"
#include "ceph_ver.h"
#include "common/DoutStreambuf.h"
#include "config.h"
#include "debug.h"
#include "Mutex.h"
#include "Clock.h"
#include "ceph_ver.h"
#include <errno.h>
#include <fstream>
#include <iostream>
using namespace std;
// debug output
std::ostream *_dout = NULL;
DoutStreambuf <char> *_doss = NULL;
bool _dout_need_open = true;
Mutex _dout_lock("_dout_lock", false, false /* no lockdep */);
#define _STR(x) #x
#define STRINGIFY(x) _STR(x)
// debug output
Mutex _dout_lock("_dout_lock", false, false /* no lockdep */);
ostream *_dout = &std::cout;
char _dout_dir[PATH_MAX] = {0};
char _dout_symlink_dir[PATH_MAX] = {0};
char _dout_file[PATH_MAX] = {0};
char _dout_path[PATH_MAX] = {0};
char _dout_rank_symlink_path[PATH_MAX] = {0};
char _dout_name_symlink_path[PATH_MAX] = {0};
char *_dout_symlink_target = 0; // _dout_path or _dout_file
bool _dout_is_open = false;
bool _dout_need_open = true;
std::ofstream _dout_out;
static void normalize_relative(const char *from, char *to, int tolen)
{
if (from[0] == '/')
strncpy(to, from, tolen);
else {
char *c = getcwd(to, tolen);
assert(c);
strncat(to, "/", tolen);
strncat(to, from, tolen);
}
}
static void build_log_paths()
{
if (g_conf.log_file && g_conf.log_file[0]) {
normalize_relative(g_conf.log_file, _dout_path, sizeof(_dout_path));
} else {
if (g_conf.log_per_instance) {
char hostname[80];
gethostname(hostname, 79);
snprintf(_dout_file, sizeof(_dout_file), "%s.%d", hostname, getpid());
} else {
snprintf(_dout_file, sizeof(_dout_file), "%s.%s.log", g_conf.type, g_conf.id);
}
snprintf(_dout_path, sizeof(_dout_path), "%s/%s", _dout_dir, _dout_file);
}
}
static bool log_to_file()
{
return (g_conf.log_dir || g_conf.log_file) && !g_conf.log_to_stdout;
}
static int create_symlink(const char *from)
{
::unlink(from);
int r = ::symlink(_dout_symlink_target, from);
if (r) {
char buf[80];
*_dout << "---- " << getpid() << " failed to symlink " << _dout_symlink_target
<< " from " << from
<< ": " << strerror_r(errno, buf, sizeof(buf)) << std::endl;
}
return r;
}
static void rotate_file(const char *fn, int max)
{
char a[200], b[200];
// rotate out old
int n = 0;
while (1) {
struct stat st;
snprintf(a, sizeof(a), "%s.%lld", fn, (long long)n);
if (::lstat(a, &st) != 0)
break;
n++;
}
while (n >= 0) {
if (n)
snprintf(a, sizeof(a), "%s.%lld", fn, (long long)n-1);
else
snprintf(a, sizeof(a), "%s", fn);
if (n >= max) {
::unlink(a);
*_dout << "---- " << getpid() << " removed " << a << " ----" << std::endl;
} else {
snprintf(b, sizeof(b), "%s.%lld", fn, (long long)n);
::rename(a, b);
*_dout << "---- " << getpid() << " renamed " << a << " -> " << b << " ----" << std::endl;
}
n--;
}
}
static int create_name_symlink()
{
int r = 0;
if (log_to_file() && g_conf.log_per_instance && !(g_conf.log_file && g_conf.log_file[0])) {
snprintf(_dout_name_symlink_path, sizeof(_dout_name_symlink_path),
"%s/%s.%s", _dout_symlink_dir, g_conf.type, g_conf.id);
rotate_file(_dout_name_symlink_path, g_conf.log_sym_history);
r = create_symlink(_dout_name_symlink_path);
}
return r;
}
void _dout_open_log()
{
bool need_symlink = false;
assert(_dout_need_open);
assert(_dout_lock.is_locked());
// logging enabled?
if (!log_to_file()) {
_dout_need_open = false;
return;
if (!_doss) {
_doss = new DoutStreambuf <char>();
}
_doss->read_global_config();
if (!_dout) {
_dout = new std::ostream(_doss);
}
// calculate log dir, filename, etc.
// do this _once_.
if (!_dout_path[0]) {
// normalize paths
normalize_relative(g_conf.log_dir, _dout_dir, sizeof(_dout_dir));
if (!g_conf.log_sym_dir)
g_conf.log_sym_dir = strdup(g_conf.log_dir);
normalize_relative(g_conf.log_sym_dir, _dout_symlink_dir, sizeof(_dout_symlink_dir));
// make symlink targets absolute or relative?
if ((g_conf.log_file && g_conf.log_file[0]) ||
strcmp(_dout_symlink_dir, _dout_dir) == 0)
_dout_symlink_target = _dout_file;
else
_dout_symlink_target = _dout_path;
build_log_paths();
need_symlink = true;
}
_dout_out.close();
_dout_out.open(_dout_path, ios::out | ios::app);
if (!_dout_out.is_open()) {
std::cerr << "error opening output file " << _dout_path << std::endl;
_dout = &std::cout;
} else {
_dout_need_open = false;
_dout_is_open = true;
_dout = &_dout_out;
*_dout << g_clock.now() << " --- " << getpid() << " opened log "
<< _dout_path << " ---" << std::endl;
}
*_dout << "ceph version " << VERSION << " (commit:" << STRINGIFY(CEPH_GIT_VER) << ")" << std::endl;
if (need_symlink)
create_name_symlink();
*_dout << "ceph version " << VERSION << " (commit:"
<< STRINGIFY(CEPH_GIT_VER) << ")" << std::endl;
_dout_need_open = false;
}
int dout_rename_output_file() // after calling daemon()
int dout_handle_pid_change() // after calling daemon()
{
Mutex::Locker l(_dout_lock);
if (log_to_file() && g_conf.log_per_instance) {
char oldpath[PATH_MAX];
char hostname[80];
gethostname(hostname, 79);
strcpy(oldpath, _dout_path);
build_log_paths();
*_dout << "---- " << getpid() << " renamed log " << oldpath << " -> " << _dout_path << " ----" << std::endl;
::rename(oldpath, _dout_path);
// $type.$id symlink
if (g_conf.log_per_instance && _dout_name_symlink_path[0])
create_symlink(_dout_name_symlink_path);
if (_dout_rank_symlink_path[0])
create_symlink(_dout_rank_symlink_path);
}
return 0;
return _doss->handle_pid_change();
}
int dout_create_rank_symlink(int64_t n)
int dout_create_rank_symlink(int n)
{
Mutex::Locker l(_dout_lock);
int r = 0;
if (log_to_file() && !(g_conf.log_file && g_conf.log_file[0])) {
if (_dout_need_open)
_dout_open_log();
return _doss->create_rank_symlink(n);
}
snprintf(_dout_rank_symlink_path, sizeof(_dout_rank_symlink_path),
"%s/%s%lld", _dout_symlink_dir, g_conf.type, (long long)n);
r = create_symlink(_dout_rank_symlink_path);
}
return r;
void dout_disable_stderr()
{
_doss->set_use_stderr(false);
}
void hex2str(const char *s, int len, char *buf, int dest_len)

View File

@ -16,32 +16,43 @@
#ifndef CEPH_DEBUG_H
#define CEPH_DEBUG_H
#include "Clock.h"
#include "Mutex.h"
#include "common/DoutStreambuf.h"
#include "common/likely.h"
#include "include/assert.h"
#include "Mutex.h"
#include "Clock.h"
#include <iosfwd>
// the streams
extern std::ostream *_dout;
extern Mutex _dout_lock;
extern DoutStreambuf <char> *_doss;
extern bool _dout_need_open;
extern bool _dout_is_open;
extern Mutex _dout_lock;
extern void _dout_open_log();
extern int dout_rename_output_file(); // after calling daemon()
extern int dout_create_rank_symlink(int64_t n);
// Call when the pid changes. For example, after calling daemon().
extern int dout_handle_pid_change();
// Skip output to stderr.
extern void dout_disable_stderr();
extern int dout_create_rank_symlink(int n);
static inline void _dout_begin_line(int prio) {
_dout_lock.Lock();
if (unlikely(_dout_need_open))
_dout_open_log();
*_dout << g_clock.now() << " " << std::hex << pthread_self() << std::dec << " ";
// Put priority information into dout
_doss->sputc(1);
_doss->sputc(prio + 11);
// Some information that goes in every dout message
*_dout << g_clock.now() << " " << std::hex << pthread_self()
<< std::dec << " ";
}
static inline void _dout_end_line() {
_dout_lock.Unlock();
}

View File

@ -2377,6 +2377,7 @@ int SimpleMessenger::start(bool nodaemon)
<< dendl;
}
dout(1) << "messenger.start daemonizing" << dendl;
dout_disable_stderr();
if (1) {
daemon(1, 0);
@ -2398,7 +2399,7 @@ int SimpleMessenger::start(bool nodaemon)
::chdir(g_conf.chdir);
}
dout_rename_output_file();
dout_handle_pid_change();
}
// go!