Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / log / Entry.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef __CEPH_LOG_ENTRY_H
5 #define __CEPH_LOG_ENTRY_H
6
7 #include "include/utime.h"
8 #include "common/PrebufferedStreambuf.h"
9 #include <pthread.h>
10 #include <string>
11
12
13 namespace ceph {
14 namespace logging {
15
16 struct Entry {
17   utime_t m_stamp;
18   pthread_t m_thread;
19   short m_prio, m_subsys;
20   Entry *m_next;
21
22   PrebufferedStreambuf m_streambuf;
23   size_t m_buf_len;
24   size_t* m_exp_len;
25   char m_static_buf[1];
26
27   Entry()
28     : m_thread(0), m_prio(0), m_subsys(0),
29       m_next(NULL),
30       m_streambuf(m_static_buf, sizeof(m_static_buf)),
31       m_buf_len(sizeof(m_static_buf)),
32       m_exp_len(NULL)
33   {}
34   Entry(utime_t s, pthread_t t, short pr, short sub,
35   const char *msg = NULL)
36       : m_stamp(s), m_thread(t), m_prio(pr), m_subsys(sub),
37         m_next(NULL),
38         m_streambuf(m_static_buf, sizeof(m_static_buf)),
39         m_buf_len(sizeof(m_static_buf)),
40         m_exp_len(NULL)
41     {
42       if (msg) {
43         ostream os(&m_streambuf);
44         os << msg;
45       }
46     }
47   Entry(utime_t s, pthread_t t, short pr, short sub, char* buf, size_t buf_len, size_t* exp_len,
48         const char *msg = NULL)
49     : m_stamp(s), m_thread(t), m_prio(pr), m_subsys(sub),
50       m_next(NULL),
51       m_streambuf(buf, buf_len),
52       m_buf_len(buf_len),
53       m_exp_len(exp_len)
54   {
55     if (msg) {
56       ostream os(&m_streambuf);
57       os << msg;
58     }
59   }
60
61   // function improves estimate for expected size of message
62   void hint_size() {
63     if (m_exp_len != NULL) {
64       size_t size = m_streambuf.size();
65       if (size > __atomic_load_n(m_exp_len, __ATOMIC_RELAXED)) {
66         //log larger then expected, just expand
67         __atomic_store_n(m_exp_len, size + 10, __ATOMIC_RELAXED);
68       }
69       else {
70         //asymptotically adapt expected size to message size
71         __atomic_store_n(m_exp_len, (size + 10 + m_buf_len*31) / 32, __ATOMIC_RELAXED);
72       }
73     }
74   }
75
76   void set_str(const std::string &s) {
77     ostream os(&m_streambuf);
78     os << s;
79   }
80
81   std::string get_str() const {
82     return m_streambuf.get_str();
83   }
84
85   // returns current size of content
86   size_t size() const {
87     return m_streambuf.size();
88   }
89
90   // extracts up to avail chars of content
91   int snprintf(char* dst, size_t avail) const {
92     return m_streambuf.snprintf(dst, avail);
93   }
94 };
95
96 }
97 }
98
99 #endif