-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntry.h
99 lines (85 loc) · 2.3 KB
/
Entry.h
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef __CEPH_LOG_ENTRY_H
#define __CEPH_LOG_ENTRY_H
#include "include/utime.h"
#include "common/PrebufferedStreambuf.h"
#include <pthread.h>
#include <string>
namespace ceph {
namespace log {
struct Entry {
utime_t m_stamp;
pthread_t m_thread;
short m_prio, m_subsys;
Entry *m_next;
PrebufferedStreambuf m_streambuf;
size_t m_buf_len;
size_t* m_exp_len;
char m_static_buf[1];
Entry()
: m_thread(0), m_prio(0), m_subsys(0),
m_next(NULL),
m_streambuf(m_static_buf, sizeof(m_static_buf)),
m_buf_len(sizeof(m_static_buf)),
m_exp_len(NULL)
{}
Entry(utime_t s, pthread_t t, short pr, short sub,
const char *msg = NULL)
: m_stamp(s), m_thread(t), m_prio(pr), m_subsys(sub),
m_next(NULL),
m_streambuf(m_static_buf, sizeof(m_static_buf)),
m_buf_len(sizeof(m_static_buf)),
m_exp_len(NULL)
{
if (msg) {
ostream os(&m_streambuf);
os << msg;
}
}
Entry(utime_t s, pthread_t t, short pr, short sub, char* buf, size_t buf_len, size_t* exp_len,
const char *msg = NULL)
: m_stamp(s), m_thread(t), m_prio(pr), m_subsys(sub),
m_next(NULL),
m_streambuf(buf, buf_len),
m_buf_len(buf_len),
m_exp_len(exp_len)
{
if (msg) {
ostream os(&m_streambuf);
os << msg;
}
}
// function improves estimate for expected size of message
void hint_size() {
if (m_exp_len != NULL) {
size_t size = m_streambuf.size();
if (size > __atomic_load_n(m_exp_len, __ATOMIC_RELAXED)) {
//log larger then expected, just expand
__atomic_store_n(m_exp_len, size + 10, __ATOMIC_RELAXED);
}
else {
//asymptotically adapt expected size to message size
__atomic_store_n(m_exp_len, (size + 10 + m_buf_len*31) / 32, __ATOMIC_RELAXED);
}
}
}
void set_str(const std::string &s) {
ostream os(&m_streambuf);
os << s;
}
std::string get_str() const {
return m_streambuf.get_str();
}
// returns current size of content
size_t size() const {
return m_streambuf.size();
}
// extracts up to avail chars of content
int snprintf(char* dst, size_t avail) const {
return m_streambuf.snprintf(dst, avail);
}
};
}
}
#endif