libdballe  7.6
db/trace.h
1 /*
2  * dballe/db/trace - Trace and measure DB queries
3  *
4  * Copyright (C) 2015 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 #ifndef DBALLE_DB_TRACE_H
22 #define DBALLE_DB_TRACE_H
23 
24 #include <dballe/core/json.h>
25 #include <wreport/varinfo.h>
26 #include <sys/types.h>
27 #include <vector>
28 #include <sstream>
29 #include <memory>
30 #include <ctime>
31 
32 namespace dballe {
33 struct Query;
34 
35 namespace db {
36 
37 class TraceOp;
38 
39 class Trace
40 {
41 protected:
42  // Command line used to start the current process
43  std::vector<std::string> argv;
44 
45  // Process ID of the current process (cached getpid() result)
46  pid_t pid;
47 
48  // Database connection URL
49  std::string db_url;
50 
51  // JSON output buffer, holding one JSON record
52  std::stringstream json_buf;
53 
54  // JSON serializer
55  core::JSONWriter writer;
56 
57  // Output file name
58  std::string out_fname;
59 
60  // Output file
61  FILE* out = 0;
62 
63  // Populate argv
64  void read_argv();
65 
66  // Cancel the current output, resetting json_buf
67  void output_abort();
68 
69  // Flush the current output, then reset json_buf
70  void output_flush();
71 
72 
73 public:
74  typedef std::unique_ptr<TraceOp> Tracer;
75 
76  Trace();
77  ~Trace();
78 
79  Tracer trace_connect(const std::string& url);
80  Tracer trace_reset(const char* repinfo_file=0);
81  Tracer trace_remove_station_data(const Query& query);
82  Tracer trace_remove(const Query& query);
83  Tracer trace_remove_all();
84  Tracer trace_vacuum();
85  Tracer trace_query_stations(const Query& query);
86  Tracer trace_query_station_data(const Query& query);
87  Tracer trace_query_data(const Query& query);
88  Tracer trace_query_summary(const Query& query);
89  Tracer trace_export_msgs(const Query& query);
90 
91  friend class TraceOp;
92 };
93 
94 class TraceOp
95 {
96 protected:
97  Trace* trace = 0;
98  clock_t start;
99 
100 public:
101  TraceOp();
102  TraceOp(Trace& trace, const char* operation);
103  ~TraceOp();
104 
105  void done();
106 
107  template<typename T>
108  void add_list(const char* key, const T& val)
109  {
110  trace->writer.add(key);
111  trace->writer.add_list(val);
112  }
113 
114  void add_null(const char* key)
115  {
116  trace->writer.add(key);
117  trace->writer.add_null();
118  }
119 
120  template<typename T>
121  void add(const char* key, const T& val)
122  {
123  trace->writer.add(key);
124  trace->writer.add(val);
125  }
126 
127  void add_query(const Query& query);
128 };
129 
130 
131 }
132 }
133 
134 #endif
Definition: db/trace.h:39
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: db/trace.h:94
Query used to filter DB-All.e data.
Definition: query.h:14
JSON serializer.
Definition: json.h:23