summaryrefslogtreecommitdiff
path: root/src/common/debug.cc
blob: 10cd6ceed9ec5ad7cf083706100d50da5285cd51 (plain)
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
#include "Mutex.h"
#include "ceph_ver.h"
#include "common/DoutStreambuf.h"
#include "config.h"
#include "debug.h"

#include <errno.h>
#include <fstream>
#include <iostream>

// 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)

void _dout_open_log()
{
  assert(_dout_need_open);
  assert(_dout_lock.is_locked());

  if (!_doss) {
    _doss = new DoutStreambuf <char>();
  }
  _doss->read_global_config();
  if (!_dout) {
    _dout = new std::ostream(_doss);
  }

  *_dout << "ceph version " << VERSION << " (commit:"
	 << STRINGIFY(CEPH_GIT_VER) << ")" << std::endl;
  _dout_need_open = false;
}

int dout_handle_daemonize()
{
  Mutex::Locker l(_dout_lock);
  _doss->handle_stdout_closed();
  _doss->handle_stderr_closed();
  return _doss->handle_pid_change();
}

int dout_create_rank_symlink(int n)
{
  Mutex::Locker l(_dout_lock);
  return _doss->create_rank_symlink(n);
}

void hex2str(const char *s, int len, char *buf, int dest_len)
{
  int pos = 0;
  for (int i=0; i<len && pos<dest_len; i++) {
    if (i && !(i%8))
      pos += snprintf(&buf[pos], dest_len-pos, " ");
    if (i && !(i%16))
      pos += snprintf(&buf[pos], dest_len-pos, "\n");
    pos += snprintf(&buf[pos], dest_len-pos, "%.2x ", (int)(unsigned char)s[i]);
  }
}

void hexdump(string msg, const char *s, int len)
{
  int buf_len = len*4;
  char buf[buf_len];
  hex2str(s, len, buf, buf_len);
  generic_dout(0) << msg << ":\n" << buf << dendl;
}