]> granicus.if.org Git - icinga2/blob - lib/base/utility.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / utility.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef UTILITY_H
4 #define UTILITY_H
5
6 #include "base/i2-base.hpp"
7 #include "base/string.hpp"
8 #include "base/array.hpp"
9 #include "base/threadpool.hpp"
10 #include <boost/thread/tss.hpp>
11 #include <typeinfo>
12 #include <vector>
13
14 namespace icinga
15 {
16
17 #ifdef _WIN32
18 #define MS_VC_EXCEPTION 0x406D1388
19
20 #       pragma pack(push, 8)
21 struct THREADNAME_INFO
22 {
23         DWORD dwType;
24         LPCSTR szName;
25         DWORD dwThreadID;
26         DWORD dwFlags;
27 };
28 #       pragma pack(pop)
29 #endif
30
31 enum GlobType
32 {
33         GlobFile = 1,
34         GlobDirectory = 2
35 };
36
37 /**
38  * Helper functions.
39  *
40  * @ingroup base
41  */
42 class Utility
43 {
44 public:
45         static String DemangleSymbolName(const String& sym);
46         static String GetTypeName(const std::type_info& ti);
47         static String GetSymbolName(const void *addr);
48
49         static bool Match(const String& pattern, const String& text);
50         static bool CidrMatch(const String& pattern, const String& ip);
51
52         static String DirName(const String& path);
53         static String BaseName(const String& path);
54
55         static String GetEnv(const String& key);
56
57         static void NullDeleter(void *);
58
59         static double GetTime();
60
61         static pid_t GetPid();
62
63         static void Sleep(double timeout);
64
65         static String NewUniqueID();
66
67         static bool Glob(const String& pathSpec, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
68         static bool GlobRecursive(const String& path, const String& pattern, const std::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
69         static void MkDir(const String& path, int mode);
70         static void MkDirP(const String& path, int mode);
71         static bool SetFileOwnership(const String& file, const String& user, const String& group);
72
73         static void QueueAsyncCallback(const std::function<void ()>& callback, SchedulerPolicy policy = DefaultScheduler);
74
75         static String NaturalJoin(const std::vector<String>& tokens);
76         static String Join(const Array::Ptr& tokens, char separator, bool escapeSeparator = true);
77
78         static String FormatDuration(double duration);
79         static String FormatDateTime(const char *format, double ts);
80         static String FormatErrorNumber(int code);
81
82 #ifndef _WIN32
83         static void SetNonBlocking(int fd, bool nb = true);
84         static void SetCloExec(int fd, bool cloexec = true);
85 #endif /* _WIN32 */
86
87         static void SetNonBlockingSocket(SOCKET s, bool nb = true);
88
89         static String EscapeShellCmd(const String& s);
90         static String EscapeShellArg(const String& s);
91 #ifdef _WIN32
92         static String EscapeCreateProcessArg(const String& arg);
93 #endif /* _WIN32 */
94
95         static String EscapeString(const String& s, const String& chars, const bool illegal);
96         static String UnescapeString(const String& s);
97
98         static void SetThreadName(const String& name, bool os = true);
99         static String GetThreadName();
100
101         static unsigned long SDBM(const String& str, size_t len = String::NPos);
102
103         static int CompareVersion(const String& v1, const String& v2);
104
105         static int Random();
106
107         static String GetHostName();
108         static String GetFQDN();
109
110         static tm LocalTime(time_t ts);
111
112         static bool PathExists(const String& path);
113
114         static void Remove(const String& path);
115         static void RemoveDirRecursive(const String& path);
116         static void CopyFile(const String& source, const String& target);
117         static void RenameFile(const String& source, const String& target);
118
119         static Value LoadJsonFile(const String& path);
120         static void SaveJsonFile(const String& path, int mode, const Value& value);
121
122         static String GetPlatformKernel();
123         static String GetPlatformKernelVersion();
124         static String GetPlatformName();
125         static String GetPlatformVersion();
126         static String GetPlatformArchitecture();
127
128         static String ValidateUTF8(const String& input);
129
130         static String CreateTempFile(const String& path, int mode, std::fstream& fp);
131
132 #ifdef _WIN32
133         static String GetIcingaInstallPath();
134         static String GetIcingaDataPath();
135 #endif /* _WIN32 */
136
137         static String GetFromEnvironment(const String& env);
138
139         static bool ComparePasswords(const String& enteredPassword, const String& actualPassword);
140
141 #ifdef I2_DEBUG
142         static void SetTime(double);
143         static void IncrementTime(double);
144 #endif /* I2_DEBUG */
145
146 private:
147         Utility();
148
149 #ifdef _WIN32
150         static int MksTemp (char *tmpl);
151 #endif /* _WIN32 */
152
153 #ifdef I2_DEBUG
154         static double m_DebugTime;
155 #endif /* I2_DEBUG */
156
157         static boost::thread_specific_ptr<String> m_ThreadName;
158         static boost::thread_specific_ptr<unsigned int> m_RandSeed;
159 };
160
161 }
162
163 #endif /* UTILITY_H */