00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef APP_UTIL_H
00024 #define APP_UTIL_H
00025
00026 #include <time.h>
00027 #include <libintl.h>
00028 #include <string.h>
00029 #include <cstdarg>
00030 #include <cstdio>
00031 #include <fstream>
00032 #include <sstream>
00033 #include <locale>
00034 #include <string>
00035 #include <list>
00036 #include <map>
00037
00038 using std::string;
00039
00040 namespace storage
00041 {
00042
00043 class AsciiFile;
00044
00045 bool searchFile(AsciiFile& File_Cr, string Pat_Cv, string& Line_Cr);
00046 bool searchFile(AsciiFile& File_Cr, string Pat_Cv, string& Line_Cr,
00047 int& StartLine_ir);
00048 void createPath(string Path_Cv);
00049 bool checkNormalFile(string Path_Cv);
00050 bool checkDir(string Path_Cv);
00051
00052 string dupDash(const string& s);
00053 string extractNthWord(int Num_iv, const string& Line_Cv, bool GetRest_bi = false);
00054 std::list<string> splitString( const string& s, const string& delChars=" \t\n",
00055 bool multipleDelim=true, bool skipEmpty=true,
00056 const string& quotes="" );
00057 string mergeString( const std::list<string>& l, const string& del=" " );
00058 std::map<string,string> makeMap( const std::list<string>& l,
00059 const string& delim = "=",
00060 const string& removeSur = " \t\n" );
00061
00062 bool readlink(const string& path, string& buf);
00063
00064 string udevAppendPart(const string&, unsigned num);
00065
00066 void getUdevMap(const char* path, std::map<string, std::list<string>>& m);
00067 void getRevUdevMap(const char* path, std::map<string, string>& m);
00068
00069 string normalizeDevice( const string& dev );
00070 void normalizeDevice( string& dev );
00071 string undevDevice( const string& dev );
00072 void undevDevice( string& dev );
00073 bool isNfsDev( const string& dev );
00074 unsigned getMajorDevices( const string& driver );
00075
00076
00077 template<class StreamType>
00078 void classic(StreamType& stream)
00079 {
00080 stream.imbue(std::locale::classic());
00081 }
00082
00083
00084 enum LogLevel { DEBUG, MILESTONE, WARNING, ERROR };
00085
00086 void createLogger(const string& component, const string& name,
00087 const string& logpath, const string& logfile);
00088
00089 bool testLogLevel(LogLevel level);
00090
00091 void logMsg(LogLevel level, const char* file, unsigned line,
00092 const char* func, const string& str);
00093
00094 void logMsgVaArgs(LogLevel level, const char* file, unsigned line,
00095 const char* func, const char* format, ...)
00096 __attribute__ ((format(printf, 5, 6)));
00097
00098 void prepareLogStream(std::ostringstream& s);
00099
00100 #define y2debug(format, ...) \
00101 logMsgVaArgs(storage::DEBUG, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
00102 #define y2milestone(format, ...) \
00103 logMsgVaArgs(storage::MILESTONE, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
00104 #define y2warning(format, ...) \
00105 logMsgVaArgs(storage::WARNING, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
00106 #define y2error(format, ...) \
00107 logMsgVaArgs(storage::ERROR, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__)
00108
00109 #define y2deb(op) y2log_op(storage::DEBUG, __FILE__, __LINE__, __FUNCTION__, op)
00110 #define y2mil(op) y2log_op(storage::MILESTONE, __FILE__, __LINE__, __FUNCTION__, op)
00111 #define y2war(op) y2log_op(storage::WARNING, __FILE__, __LINE__, __FUNCTION__, op)
00112 #define y2err(op) y2log_op(storage::ERROR, __FILE__, __LINE__, __FUNCTION__, op)
00113
00114 #define y2log_op(level, file, line, function, op) \
00115 do { \
00116 if (storage::testLogLevel(level)) \
00117 { \
00118 std::ostringstream __buf; \
00119 storage::prepareLogStream(__buf); \
00120 __buf << op; \
00121 storage::logMsg(level, file, line, function, __buf.str()); \
00122 } \
00123 } while (0)
00124
00125
00126 string sformat(const char* format, ...);
00127
00128
00129 string byteToHumanString(unsigned long long size, bool classic, int precision,
00130 bool omit_zeroes);
00131
00132 bool humanStringToByte(const string& str, bool classic, unsigned long long& size);
00133
00134
00135 inline const char* _(const char* msgid)
00136 {
00137 return dgettext("storage", msgid);
00138 }
00139
00140 inline const char* _(const char* msgid, const char* msgid_plural, unsigned long int n)
00141 {
00142 return dngettext("storage", msgid, msgid_plural, n);
00143 }
00144
00145
00146 extern const string app_ws;
00147
00148 }
00149
00150 #endif