Line data Source code
1 : // Snap Websites Server -- snapwebsites flag functionality
2 : // Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // https://snapwebsites.org/
5 : // contact@m2osw.com
6 : //
7 : // This program is free software; you can redistribute it and/or modify
8 : // it under the terms of the GNU General Public License as published by
9 : // the Free Software Foundation; either version 2 of the License, or
10 : // (at your option) any later version.
11 : //
12 : // This program is distributed in the hope that it will be useful,
13 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : // GNU General Public License for more details.
16 : //
17 : // You should have received a copy of the GNU General Public License
18 : // along with this program; if not, write to the Free Software
19 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 : #pragma once
21 :
22 : // lib snapwebsites
23 : //
24 : #include <snapwebsites/snap_exception.h>
25 :
26 : // C++ lib
27 : //
28 : #include <memory>
29 : #include <set>
30 : #include <vector>
31 :
32 :
33 : namespace snap
34 : {
35 :
36 0 : class flags_exception : public snap_exception
37 : {
38 : public:
39 0 : flags_exception(char const * what_msg) : snap_exception("flags", what_msg) {}
40 : flags_exception(std::string const & what_msg) : snap_exception("flags", what_msg) {}
41 : flags_exception(QString const & what_msg) : snap_exception("flags", what_msg) {}
42 : };
43 :
44 0 : class flags_exception_invalid_parameter : public flags_exception
45 : {
46 : public:
47 0 : flags_exception_invalid_parameter(char const * what_msg) : flags_exception(what_msg) {}
48 : flags_exception_invalid_parameter(std::string const & what_msg) : flags_exception(what_msg) {}
49 : flags_exception_invalid_parameter(QString const & what_msg) : flags_exception(what_msg) {}
50 : };
51 :
52 0 : class flags_exception_too_many_flags : public flags_exception
53 : {
54 : public:
55 0 : flags_exception_too_many_flags(char const * what_msg) : flags_exception(what_msg) {}
56 : flags_exception_too_many_flags(std::string const & what_msg) : flags_exception(what_msg) {}
57 : flags_exception_too_many_flags(QString const & what_msg) : flags_exception(what_msg) {}
58 : };
59 :
60 :
61 :
62 :
63 :
64 0 : class snap_flag
65 : {
66 : public:
67 : typedef std::shared_ptr<snap_flag> pointer_t;
68 : typedef std::vector<pointer_t> vector_t;
69 :
70 : typedef std::set<std::string> tag_list_t;
71 :
72 : enum class state_t
73 : {
74 : STATE_UP, // flag is an error
75 : STATE_DOWN // flag file gets deleted
76 : };
77 :
78 : snap_flag(std::string const & unit, std::string const & section, std::string const & name);
79 : snap_flag(std::string const & filename);
80 :
81 : snap_flag & set_state(state_t state);
82 : snap_flag & set_source_file(std::string const & source_file);
83 : snap_flag & set_function(std::string const & function);
84 : snap_flag & set_line(int line);
85 : snap_flag & set_message(std::string const & message);
86 : snap_flag & set_message(QString const & message);
87 : snap_flag & set_message(char const * message);
88 : snap_flag & set_priority(int priority);
89 : snap_flag & set_manual_down(bool manual);
90 : snap_flag & add_tag(std::string const & tag);
91 :
92 : state_t get_state() const;
93 : std::string const & get_unit() const;
94 : std::string const & get_section() const;
95 : std::string const & get_name() const;
96 : std::string const & get_filename() const;
97 : std::string const & get_source_file() const;
98 : std::string const & get_function() const;
99 : int get_line() const;
100 : std::string const & get_message() const;
101 : int get_priority() const;
102 : bool get_manual_down() const;
103 : time_t get_date() const;
104 : time_t get_modified() const;
105 : tag_list_t const & get_tags() const;
106 : std::string const & get_hostname() const;
107 : int get_count() const;
108 : std::string const & get_version() const;
109 :
110 : bool save();
111 :
112 : static vector_t load_flags();
113 :
114 : private:
115 : static void valid_name(std::string & name);
116 : static void load_flag(std::string const & filename, vector_t * result);
117 :
118 : state_t f_state = state_t::STATE_UP;
119 : std::string f_unit = std::string();
120 : std::string f_section = std::string();
121 : std::string f_name = std::string();
122 : mutable std::string f_filename = std::string();
123 : std::string f_source_file = std::string();
124 : std::string f_function = std::string();
125 : int f_line = 0;
126 : std::string f_message = std::string();
127 : int f_priority = 5;
128 : bool f_manual_down = false;
129 : time_t f_date = -1;
130 : time_t f_modified = -1;
131 : tag_list_t f_tags = tag_list_t();
132 : std::string f_hostname = std::string();
133 : int f_count = 0;
134 : std::string f_version = std::string();
135 : };
136 :
137 :
138 :
139 : #define SNAP_FLAG_UP(unit, section, name, message) \
140 : std::make_shared<snap::snap_flag>( \
141 : snap::snap_flag(unit, section, name) \
142 : .set_message(message) \
143 : .set_source_file(__FILE__) \
144 : .set_function(__func__) \
145 : .set_line(__LINE__))
146 :
147 : #define SNAP_FLAG_DOWN(unit, section, name) \
148 : std::make_shared<snap::snap_flag>( \
149 : snap::snap_flag(unit, section, name) \
150 : .set_state(snap::snap_flag::state_t::STATE_DOWN) \
151 : .set_source_file(__FILE__) \
152 : .set_function(__func__) \
153 : .set_line(__LINE__))
154 :
155 :
156 :
157 : }
158 : // snap namespace
159 : // vim: ts=4 sw=4 et
|