'@uptime' macro for unixes is implemented using /proc filesystem. If uptime isn't supported then writes message to log

This commit is contained in:
Stas Degteff 2007-02-04 18:06:33 +00:00
parent 615846e57d
commit 08e75a4a4d

View File

@ -684,7 +684,6 @@ void TokenXlat(int mode, std::string &input, GMsg* msg, GMsg* oldmsg, int __orig
continue; continue;
} }
#ifdef __WIN32__
if (strnieql(it2str(input, dst), "@uptime", 7)) if (strnieql(it2str(input, dst), "@uptime", 7))
{ {
size_t days = 0; size_t days = 0;
@ -700,24 +699,50 @@ void TokenXlat(int mode, std::string &input, GMsg* msg, GMsg* oldmsg, int __orig
QueryPerformanceFrequency(&frequency); QueryPerformanceFrequency(&frequency);
seconds = counter.QuadPart / frequency.QuadPart; seconds = counter.QuadPart / frequency.QuadPart;
useconds = size_t(double(counter.QuadPart % frequency.QuadPart)*1000 / frequency.QuadPart); useconds = size_t(double(counter.QuadPart % frequency.QuadPart)*1000 / frequency.QuadPart);
#elif defined(__unix__)
FILE *fd = fopen("/proc/uptime","r");
if( fd )
{
char s[20];
if( fgets(s, sizeof(s), fd) )
{
if( sizeof(size_t) == sizeof(long) )
sscanf(s, "%lu.%2lu", &seconds, &useconds);
/* to future 64-bit unixtime
else if( sizeof(size_t) == sizeof(long long) )
sscanf(s, "%Lu.%2Lu", &seconds, &useconds);
*/
else if( sizeof(size_t) == sizeof(unsigned) )
sscanf(s, "%u.%2u", &seconds, &useconds);
}
}
#else
LOG.printf("Macros \"@uptime\" is not suppoted on platform %s!", __gver_platform__);
#endif #endif
days = seconds/(60*60*24); seconds %= 60*60*24; if( seconds+useconds>0 )
hours = seconds/(60*60); seconds %= 60*60; {
minutes = seconds/60; seconds %= 60; days = seconds/(60*60*24); seconds %= 60*60*24;
hours = seconds/(60*60); seconds %= 60*60;
minutes = seconds/60; seconds %= 60;
std::string uptime(LNG->Uptime); std::string uptime(LNG->Uptime);
FormatString( FormatString( FormatString( FormatString( FormatString( FormatString( FormatString( FormatString( FormatString( FormatString(
uptime, "%days", days), uptime, "%days", days),
"%hours", hours), "%hours", hours),
"%minutes", minutes), "%minutes", minutes),
"%seconds", seconds), "%seconds", seconds),
"%useconds", useconds); "%useconds", useconds);
tokenxchg(input, dst, "@uptime", uptime.c_str()); tokenxchg(input, dst, "@uptime", uptime.c_str());
}
else
{
LOG.printf("Can't determine uptime value, set @uptime empty.");
tokenxchg(input, dst, "@uptime", "");
}
} }
#endif
} }
dst++; dst++;
} }