00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <cerrno>
00027 #include <cstdio>
00028 #include <mpcl/system/posix_1/system.hh>
00029 #include <sched.h>
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <unistd.h>
00033
00034
00036 namespace mpcl
00037 {
00038
00039
00040
00041
00042
00044 static const char _kcDirectorySeparator = '/';
00045
00047 static const char _kcPathSeparator = ':';
00048
00049 }
00050
00051
00052
00053
00054
00055
00056 void mpcl::system::TSystem::
00057 sleep (std::size_t zSECONDS, std::size_t zNANOSECONDS) const
00058 {
00059 struct timespec tTimespec;
00060
00061 tTimespec.tv_sec = zSECONDS;
00062 tTimespec.tv_nsec = zNANOSECONDS;
00063 nanosleep (&tTimespec, NULL);
00064 }
00065
00066
00067 void mpcl::system::TSystem::
00068 yield (void) const
00069 {
00070 sched_yield();
00071 }
00072
00073
00074 int mpcl::system::TSystem::
00075 getProcessId (void) const
00076 {
00077 return getpid();
00078 }
00079
00080
00081 mpcl::text::TString mpcl::system::TSystem::
00082 getCurrentDirectory (void) const
00083 {
00084
00085 using std::size_t;
00086 using text::TString;
00087
00088 TString yCurrentDirectory;
00089 char* pcBuffer;
00090 size_t zSize = 1024;
00091
00092 pcBuffer = new char [zSize];
00093 while ( !getcwd (pcBuffer, zSize) )
00094 {
00095 if ( errno != ERANGE )
00096 {
00097 break;
00098 }
00099 else
00100 {
00101 zSize += 1024;
00102 delete[] pcBuffer;
00103 pcBuffer = new char [zSize];
00104 }
00105 }
00106 if ( pcBuffer )
00107 {
00108 yCurrentDirectory = pcBuffer;
00109 delete[] pcBuffer;
00110 }
00111 return yCurrentDirectory;
00112
00113 }
00114
00115
00116 std::size_t mpcl::system::TSystem::
00117 getFileSize (const char* pkcFILE_NAME) const
00118 throw (TErrorException)
00119 {
00120
00121 struct stat sStat;
00122 std::size_t zSize = 0;
00123
00124 if ( stat (pkcFILE_NAME, &sStat) )
00125 {
00126 throw TErrorException (GetErrorMessage(), __FILE__, __LINE__);
00127 }
00128 else
00129 {
00130 zSize = sStat.st_size;
00131 }
00132 return zSize;
00133
00134 }
00135
00136
00137 char mpcl::system::TSystem::
00138 getDirectorySeparator (void) const
00139 {
00140 return _kcDirectorySeparator;
00141 }
00142
00143
00144 char mpcl::system::TSystem::
00145 getPathSeparator (void) const
00146 {
00147 return _kcPathSeparator;
00148 }
00149
00150
00151 void mpcl::system::TSystem::
00152 writeLogEntry ( const std::string& rkyMESSAGE ,
00153 ELogLevel eLEVEL ,
00154 ELogFacility eFACILITY ) const
00155 {
00156
00157 int iPosixFacility = LOG_USER;
00158 int iPosixLevel = LOG_NOTICE;
00159
00160 switch (eFACILITY)
00161 {
00162 case eAuthPriv: iPosixFacility = LOG_AUTHPRIV; break;
00163 case eCron: iPosixFacility = LOG_CRON; break;
00164 case eDaemon: iPosixFacility = LOG_DAEMON; break;
00165 case eKern: iPosixFacility = LOG_KERN; break;
00166 case eLocal0: iPosixFacility = LOG_LOCAL0; break;
00167 case eLocal1: iPosixFacility = LOG_LOCAL1; break;
00168 case eLocal2: iPosixFacility = LOG_LOCAL2; break;
00169 case eLocal3: iPosixFacility = LOG_LOCAL3; break;
00170 case eLocal4: iPosixFacility = LOG_LOCAL4; break;
00171 case eLocal5: iPosixFacility = LOG_LOCAL5; break;
00172 case eLocal6: iPosixFacility = LOG_LOCAL6; break;
00173 case eLocal7: iPosixFacility = LOG_LOCAL7; break;
00174 case eLpr: iPosixFacility = LOG_LPR; break;
00175 case eMail: iPosixFacility = LOG_MAIL; break;
00176 case eNews: iPosixFacility = LOG_NEWS; break;
00177 case eUser: iPosixFacility = LOG_USER; break;
00178 case eUucp: iPosixFacility = LOG_UUCP; break;
00179 }
00180 switch (eLEVEL)
00181 {
00182 case eEmerg: iPosixLevel = LOG_EMERG; break;
00183 case eAlert: iPosixLevel = LOG_ALERT; break;
00184 case eCrit: iPosixLevel = LOG_CRIT; break;
00185 case eErr: iPosixLevel = LOG_ERR; break;
00186 case eWarning: iPosixLevel = LOG_WARNING; break;
00187 case eNotice: iPosixLevel = LOG_NOTICE; break;
00188 case eInfo: iPosixLevel = LOG_INFO; break;
00189 case eDebug: iPosixLevel = LOG_DEBUG; break;
00190 }
00191 syslog (iPosixLevel | iPosixFacility, rkyMESSAGE.c_str());
00192
00193 }
00194
00195
00197 namespace mpcl
00198 {
00199
00201 namespace system
00202 {
00203
00204
00205
00206
00207
00208 TSystem tSystem;
00209
00210 }
00211
00212 }