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