]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/output.h
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / output.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Written by: Eric Bollengier, December MMXIII
21  */
22
23 #ifndef OUTPUT_H
24 #define OUTPUT_H
25
26 #include "bacula.h"
27
28 typedef enum {
29    OT_INT,                      /* Integer */
30    OT_SIZE,                     /* int64 size */
31    OT_PINT32,                   /* Uint32 */
32    OT_INT32,
33    OT_PINT64,                   /* Uint64 */
34    OT_INT64,
35    OT_STRING,
36    OT_BTIME,                    /* btime_t */
37    OT_UTIME,                    /* utime_t */
38    OT_JOBTYPE,
39    OT_JOBLEVEL,
40    OT_JOBSTATUS,
41    OT_PLUGINS,                  /* Plugin alist */
42    OT_RATIO,                    /* Double %.2f format */
43    OT_ALIST_STR,
44
45    OT_END,                      /* Last operator (no extra arg) */
46    OT_START_OBJ,                /* Skip a line to start a new object (no extra arg) */
47    OT_END_OBJ,                  /* Skip a line to end current object (no extra arg) */
48    OT_CLEAR                     /* truncate current buffer (no extra arg) */
49 } OutputType;
50
51 /* Keep the same order for get_options/parse_options */
52 typedef enum {
53    OTT_TIME_ISO  = 0,
54    OTT_TIME_UNIX = 1,          /* unix time stamp */
55    OTT_TIME_NC   = 2           /* Formatted time for user display: dd-Mon hh:mm */
56 } OutputTimeType;
57
58 #define OW_DEFAULT_SEPARATOR  '\n'
59 #define OW_DEFAULT_TIMEFORMAT OTT_TIME_ISO
60
61 /* If included from output.c, mark the class as export (else, symboles are
62  * exported from all files...
63  */
64 #ifdef OUTPUT_C
65 # define OUTPUT_EXPORT DLL_IMP_EXP
66 #else
67 # define OUTPUT_EXPORT
68 #endif
69
70 class OUTPUT_EXPORT OutputWriter: public SMARTALLOC
71 {
72 private:
73    void init() {
74       buf = NULL;
75       separator  = OW_DEFAULT_SEPARATOR;
76       separator_str[0] = OW_DEFAULT_SEPARATOR;
77       separator_str[1] = 0;
78       timeformat = OW_DEFAULT_TIMEFORMAT;
79       object_separator = 0;
80       flags = 0;
81    };
82
83 protected:
84    virtual char   *get_output(va_list ap, POOLMEM **out, OutputType first);
85    void            get_buf(bool append); /* Allocate buf if needed */
86
87    int             flags;
88    char            separator;
89    char            separator_str[2];
90    char            object_separator;
91    OutputTimeType  timeformat;
92    POOLMEM        *buf;
93
94 public:
95    OutputWriter(const char *opts) {
96       init();
97       parse_options(opts);
98    };
99
100    OutputWriter() {
101       init();
102    };
103
104    virtual ~OutputWriter() {
105       free_and_null_pool_memory(buf);
106    };
107
108    /* s[ascii code]t[0-3]
109     *      ^^^        ^^
110     *    separator  time format
111     * "s43"   => + will be used as separator
112     * "s43t1" => + as separator and time as unix timestamp
113     */
114    virtual void  parse_options(const char *opts);
115    virtual char *get_options(char *dest_l128); /* MAX_NAME_LENGTH mini  */
116
117    /* Make a clear separation in the output*/
118    virtual char *start_group(const char *name, bool append=true);
119    virtual char *end_group(bool append=true);
120
121    /* Make a clear separation in the output for list*/
122    virtual char *start_list(const char *name, bool append=true);
123    virtual char *end_list(bool append=true);
124
125    /* \n by default, can be \t for example */
126    void set_separator(char sep) {
127       separator = sep;
128       separator_str[0] = sep;
129    };
130
131    void set_object_separator(char sep) {
132       object_separator = sep;
133    };
134
135    void set_time_format(OutputTimeType fmt) {
136       timeformat = fmt;
137    };
138
139 /* Usage:
140  *   get_output(&out,
141  *       OT_STRING,   "name",       "value",
142  *       OT_PINT32,   "age",        10,
143  *       OT_TIME,     "birth-date", 1120202002,
144  *       OT_PINT64,   "weight",     100,
145  *       OT_END);
146  *
147  *
148  *  "name=value\nage=10\nbirt-date=2012-01-12 10:20:00\nweight=100\n"
149  *
150  */
151
152    /* Use a user supplied buffer */
153    char *get_output(POOLMEM **out, OutputType first, ...);
154
155    /* Use the internal buffer */
156    char *get_output(OutputType first, ...);
157 };
158
159 #endif