19 static std::string
win32_message(
const std::string& message, DWORD code)
21 std::string s(message);
24 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
25 | FORMAT_MESSAGE_FROM_SYSTEM
26 | FORMAT_MESSAGE_IGNORE_INSERTS,
29 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
34 LocalFree((LPVOID)errMsg);
45 static std::string
win32_message(
unsigned int code, EXCEPTION_POINTERS * pExp)
48 _snprintf(buffer,
sizeof(buffer),
"Win32StructuredException code 0x%x pExpCode 0x%x pExpAddress %p", code, pExp->ExceptionRecord->ExceptionCode, pExp->ExceptionRecord->ExceptionAddress);
49 buffer[
sizeof(buffer)-1] =
'\0';
50 return std::string(buffer);
60 static std::string
crtl_message(
const std::string& message,
int code)
62 std::string s(message);
64 s.append(strerror(code));
74 static std::string
win32_message(
const std::string& message, DWORD code)
76 std::string s(message);
79 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
80 | FORMAT_MESSAGE_FROM_SYSTEM
81 | FORMAT_MESSAGE_IGNORE_INSERTS,
84 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
89 LocalFree((LPVOID)errMsg);
94 void getConfigStringList(std::vector<std::string>& string_list,
const std::string& config_item);
95 void getConfigIntList(std::vector<int>& int_list,
const std::string& config_item);
100 for( ; ( (ret = func()) == FALSE ) && (nretry >= 0); --nretry)
118 m_old_prio = GetThreadPriority(GetCurrentThread());
119 if (
m_old_prio == THREAD_PRIORITY_ERROR_RETURN)
121 throw std::runtime_error(
"GetThreadPriority");
123 if (SetThreadPriority(GetCurrentThread(), prio) == 0)
131 if (SetThreadPriority(GetCurrentThread(),
m_old_prio) == 0)
171 void setParameters(LONGLONG perf_counter, LONGLONG proc_freq, FILETIME filetime)
174 if ( (proc_freq != 0) && (proc_freq !=
m_proc_freq.QuadPart) )
184 void getParameters(LONGLONG& perf_counter, LONGLONG& proc_freq, FILETIME& filetime)
193 LARGE_INTEGER perf_counter, perf_elapsed;
194 ULONGLONG filetime_elapsed;
195 ULARGE_INTEGER ft_ul;
197 if (QueryPerformanceCounter(&perf_counter) == 0)
201 perf_elapsed.QuadPart = perf_counter.QuadPart -
m_perf_counter.QuadPart;
202 filetime_elapsed =
static_cast<ULONGLONG
>(0.5 + (double)perf_elapsed.QuadPart /
m_perf_to_ft);
205 ft_ul.QuadPart += filetime_elapsed;
206 ft.dwLowDateTime = ft_ul.LowPart;
207 ft.dwHighDateTime = ft_ul.HighPart;
214 tt = Poco::Timestamp::fromFileTimeNP(ft.dwLowDateTime, ft.dwHighDateTime).epochTime();
218 extern Poco::SingletonHolder<ICPClock>
g_icp_clock;
221 template <
typename T>
244 Poco::Timestamp
m_ts;
259 void start(
const char* title = NULL);
260 double info(
const char* title, std::ostream& os,
bool add_nl =
true);
262 double info(
const std::string& title,
DAEstatus& status) {
return info(title.c_str(), status); }
267 std::vector<char> buffer(len+1);
268 char* bdata = buffer.data();
269 if ( _snprintf(bdata, len+1,
"%0*d", len, number) < 0 )
271 memset(bdata,
'#', len);
285 HANDLE
createEmptyFile(
const std::string& file_name,
size_t file_size);
288 template <
typename T>
289 void findConsecutive(
const std::vector<T>& vec, T invalid_val, std::vector<int>& indexes, std::vector<int>& sizes);
291 int sendSMS(
const std::string& phone,
const std::string& message,
DAEstatus& status);
292 int sendEmail(
const std::string& sender,
const std::string& recipient,
const std::string& subject,
const std::string& content,
DAEstatus& status);
296 int findFiles(
const char* directory,
const char* pattern, std::vector<std::string>& list,
bool full_path =
true);
298 void AddEventSource( PCTSTR pszName, PCTSTR pszMessageFile, DWORD dwCategoryCount);
301 std::string
escapeXML(
const char* xml_str);
306 bool compareChecksums(
const unsigned char* md5sum1,
const unsigned char* md5sum2);
307 int updateChecksum(
unsigned char* md5sum1,
const unsigned char* md5sum2);
308 int parseSpectraRange(
const std::string& spec_range,
int& spec_from,
int& spec_to);
314 void appendToFileAsync(HANDLE h,
const char* message,
bool close_after_write =
false);
320 unsigned char in[64];
321 unsigned char digest[16];
342 throw std::runtime_error(
"ICPcritical: NULL");
380 int atomic_write(
const std::string& filename,
const std::string& old_suffix,
const std::string& new_suffix, T* data,
int n)
382 Poco::Mutex::ScopedLock _lock(*atomic_mutex);
383 std::string old_file = filename + old_suffix;
384 std::string new_file = filename + new_suffix;
385 FILE* f = _fsopen(new_file.c_str(),
"wbcN", _SH_DENYWR);
389 f = _fsopen(new_file.c_str(),
"wbcN", _SH_DENYWR);
393 if (std::fwrite(data,
sizeof(T), n, f) != n)
396 std::remove(new_file.c_str());
397 throw std::runtime_error(
"cannot write data");
399 if (std::fflush(f) != 0)
402 std::remove(new_file.c_str());
403 throw std::runtime_error(
"cannot flush data");
405 if (std::fclose(f) != 0)
407 std::remove(new_file.c_str());
408 throw std::runtime_error(
"cannot close data");
410 if (access(old_file.c_str(), 0) == 0)
414 if (access(filename.c_str(), 0) == 0)
416 RetryWIN32APIBOOL(boost::bind(MoveFile, filename.c_str(), old_file.c_str()), 10);
418 RetryWIN32APIBOOL(boost::bind(MoveFile, new_file.c_str(), filename.c_str()), 10);
423 throw std::runtime_error(
"atomic:write: cannot open file");
429 extern int flushed_write(
const std::string& file,
const void* data,
int nbytes);
433 int atomic_read(
const std::string& filename,
const std::string& old_suffix, T* data,
size_t n)
435 Poco::Mutex::ScopedLock _lock(*atomic_mutex);
436 std::string old_file = filename + old_suffix;
437 FILE* f = _fsopen(filename.c_str(),
"rbN", _SH_DENYNO);
440 f = _fsopen(old_file.c_str(),
"rbN", _SH_DENYNO);
444 size_t nread = fread(data,
sizeof(T), n, f);
467 template <
typename T>
470 return boost::extent<T>::value;
473 LONGLONG
diffFileTimes(
const FILETIME& start,
const FILETIME& finish);
476 template<
typename T1,
typename T2>
479 static const T2&
second(
const std::pair<T1, T2>& p)
483 typedef const T2& (*second_t)(
const std::pair<T1, T2>& p);
495 template <
typename T>
503 operator T() {
return sum; }
506 template <
typename T,
typename U>
509 class sum_map_values_impl
514 sum_map_values_impl() : sum(0) { }
515 void operator()(
const typename std::map<T,U>::value_type& p) { sum += p.second; }
516 operator U() {
return sum; }
518 return std::for_each(m.begin(), m.end(), sum_map_values_impl());
534 LOGSTR_DEBUG(
"Starting task " << name() <<
" thread id " << Poco::Thread::currentTid());
546 LOGSTR_DEBUG(
"Terminating task " << name() <<
" thread id " << Poco::Thread::currentTid());
551 Poco::Task::cancel();
594 LOGSTR_DEBUG(
"Creating GenericWorker thread id " << Poco::Thread::currentTid());
595 Poco::AutoPtr<Poco::Notification> pNf(
m_queue.waitDequeueNotification());
603 pNf =
m_queue.waitDequeueNotification();
605 LOGSTR_DEBUG(
"Terminating GenericWorker thread id " << Poco::Thread::currentTid());
608 virtual void doWork(T* data) = 0;
612 WriteFileProgressHandler ph;
613 m_taskmgr.addObserver(Poco::Observer<WriteFileProgressHandler, Poco::TaskProgressNotification>(ph, &WriteFileProgressHandler::onProgress));
614 m_taskmgr.addObserver(Poco::Observer<WriteFileProgressHandler, Poco::TaskFinishedNotification>(ph, &WriteFileProgressHandler::onFinished));
615 m_taskmgr.start(
new WriteFileTask(
"write 1"));
617 Poco::NotificationQueue nq;
619 Poco::ThreadPool::defaultPool().start(w1);
620 nq.enqueueNotification(
new WorkNotification());
623 Poco::Thread::sleep(100);
626 Poco::ThreadPool::defaultPool().joinAll();
634 template <
typename R>
643 Poco::ActiveMethod<R, func_t, ActiveObject>
run;
656 void operator=(
const Poco::Semaphore&) {
throw std::runtime_error(
"ScopedSemaphore"); }
665 template <
typename T>
675 Poco::Mutex::ScopedLock _lock(
m_mutex);
689 Poco::Mutex::ScopedLock _lock(
m_mutex);
694 Poco::Mutex::ScopedLock _lock(
m_mutex);
695 for(list_t::iterator it =
m_vals.begin(); it !=
m_vals.end(); ++it)
711 class poco_mempool_allocator
714 typedef std::size_t size_type;
715 typedef std::ptrdiff_t difference_type;
717 typedef const T* const_pointer;
718 typedef T& reference;
719 typedef const T& const_reference;
720 typedef T value_type;
721 template <
class U>
struct rebind {
722 typedef poco_mempool_allocator<U> other;
724 poco_mempool_allocator() throw() { }
725 poco_mempool_allocator(
const poco_mempool_allocator&) throw() { }
726 template<
class U> poco_mempool_allocator(
const poco_mempool_allocator<U>&) throw() { }
727 ~poco_mempool_allocator() throw() { }
728 pointer address(reference x)
const {
return &x; }
729 const_pointer address(const_reference x)
const {
return &x; }
730 pointer allocate(size_type n,
void* hint = 0)
732 return static_cast<T*
>(n*
sizeof(T));
734 void deallocate(pointer p, size_type n) { }
735 size_type max_size()
const throw() {
return bytes /
sizeof(T); }
736 void construct(pointer p,
const T& val)
738 new(
static_cast<void*
>(p)) T(val);
740 void destroy(pointer p)
747 bool operator==(
const poco_mempool_allocator<T>&,
const poco_mempool_allocator<T>&)
753 bool operator!=(
const poco_mempool_allocator<T>&,
const poco_mempool_allocator<T>&)
759 class poco_mempool_allocator<void>
762 typedef void* pointer;
763 typedef const void* const_pointer;
764 typedef void value_type;
765 template <
class U>
struct rebind {
766 typedef poco_mempool_allocator<U> other;
int md5sumFile(const char *filename, unsigned char *md5sum, DAEstatus &status)
void getCurrentTimeFromEpoch(time_t &tt)
void operator=(const Poco::Semaphore &)
WinsockException(const std::string &message, DWORD code)
LONGLONG diffFileTimes(const FILETIME &start, const FILETIME &finish)
finish - start, returned in 100ns units
void pollUntilTasksComplete(const Poco::TaskManager &tm, int poll_time_in_ms)
std::string checksumAsString(md5checksum_t checksum)
Poco::Timestamp::TimeDiff timeDiff(const ValueWithTimestamp &v)
int compareBuffers(const std::string &title, const isisU32_t *buffer1, const isisU32_t *buffer2, int nbuffer, DAEstatus &status)
int atomic_read(const std::string &filename, const std::string &old_suffix, T *data, size_t n)
bool compareChecksums(const unsigned char *md5sum1, const unsigned char *md5sum2)
static std::string win32_message(const std::string &message, DWORD code)
void setICPChannelReporter(DAEreport_func_t *report_func, void *report_arg)
static std::string crtl_message(const std::string &message, int code)
int DAEreport_func_t(const DAEstatus_message &mess, void *arg)
ScopedSemaphore(const Poco::Semaphore &)
std::string fileNameFromMappedView(void *pMem)
void(* overlap_complete_func_t)(void *arg)
R runImpl(const func_t &arg)
Win32Exception(const std::string &message, DWORD code)
Win32StructuredException(unsigned int code, EXCEPTION_POINTERS *pExp)
T valueDiff(const ValueWithTimestamp &v)
volatile LONG m_lockcount
off_t file_size_bytes(const std::string &filename)
return size of a file in bytes
boost::function< R()> func_t
static void RetryWIN32APIBOOL(boost::function< BOOL()> func, int nretry)
int updateChecksum(unsigned char *md5sum1, const unsigned char *md5sum2)
SECURITY_ATTRIBUTES * defaultNoInheritHandles()
#define LOGSTR_DEBUG(__arg)
Poco::Mutex * atomic_mutex
double diffFileTimesInMilliSec(const FILETIME &start, const FILETIME &finish)
finish - start in milliseconds
void(__stdcall * MD5Final_t)(MD5_CTX *)
Poco::ActiveMethod< R, func_t, ActiveObject > run
size_t array_length(const T &)
calculate number of elements in a fixed array e.g. int something[10] ot int s[] = { "a"...
HANDLE createEmptyFile(const std::string &file_name, size_t file_size)
std::string convertWindows1252ToUTF8(const std::string &win_string)
Poco::NotificationQueue & m_queue
void appendToFileAsync(HANDLE h, const char *message, bool close_after_write)
std::string padWithZeros(int number, int len)
CRTLException(const std::string &message, int code)
std::string escapeXML(const char *xml_str)
static std::string win32_message(const std::string &message, DWORD code)
void(__stdcall * MD5Init_t)(MD5_CTX *)
ThreadPriorityRaiser(int prio=THREAD_PRIORITY_ABOVE_NORMAL)
ICPCritical(CRITICAL_SECTION *pCsect, bool locked=true)
LARGE_INTEGER m_proc_freq
int findFiles(const char *directory, const char *pattern, std::vector< std::string > &list, bool full_path)
GenericWorkNotification(T *data)
Win32StructuredException(const std::string &message)
HANDLE createAppendFile(const char *filename)
#define LOGSTR_ERROR(__arg)
int loadAppConfig(const std::string &app_name)
load a config file from ../../.. i.e. from c:/labview modules/dae if we are in c:/labview modules/dae...
unsigned char md5checksum_t[16]
int sendEmail(const std::string &sender, const std::string &recipient, const std::string &subject, const std::string &content, DAEstatus &status)
double info(const char *title, std::ostream &os, bool add_nl=true)
void start(const char *title=NULL)
static std::string win32_message(unsigned int code, EXCEPTION_POINTERS *pExp)
Poco::SingletonHolder< ICPClock > g_icp_clock
void AddEventSource(PCTSTR pszName, PCTSTR pszMessageFile, DWORD dwCategoryCount)
int removeWithRetry(const char *filename)
return 0 on success, else error
boost::function< T()> func_t
virtual void doWork(T *data)=0
void getConfigIntList(std::vector< int > &int_list, const std::string &config_item)
read a ; separated list of items from a properties file and return the items as a vector<int> ...
void(__stdcall * MD5Update_t)(MD5_CTX *, const unsigned char *, unsigned int)
Poco::SharedPtr< Poco::Event > m_finish_event
struct MD5Context MD5_CTX
void setParameters(LONGLONG perf_counter, LONGLONG proc_freq, FILETIME filetime)
void operator()(const T &val)
nice idea, but problems with return type...
static const T2 & second(const std::pair< T1, T2 > &p)
int md5sumString(const void *buffer, int len, unsigned char *md5sum, DAEstatus &status)
int createEmptyFileAndClose(const std::string &file_name, size_t file_size)
Base(const std::string &logger_name="UNKNOWN")
GenericWorker(Poco::NotificationQueue &queue)
timings_t m_process_times
void getConfigStringList(std::vector< std::string > &string_list, const std::string &config_item)
read a ; separated list of items from a properties file and return the items as a vector<string> ...
U sum_map_values(const std::map< T, U > &m)
double info(const std::string &title, DAEstatus &status)
void getParameters(LONGLONG &perf_counter, LONGLONG &proc_freq, FILETIME &filetime)
void setLoggerName(const std::string &logger_name)
WinsockException(const std::string &message)
void onProgress(Poco::TaskProgressNotification *pNf)
this must have NO VIRTUAL FUNCTIONS as it is embedded inside the CRPT
std::string unescapeXML(const char *xml_str)
void getCurrentTimeAsFiletime(FILETIME &ft)
void registerExtraLoggerChannels()
ScopedSemaphore(Poco::Semaphore &sem)
void onFinished(Poco::TaskFinishedNotification *pNf)
CRITICAL_SECTION * m_critical
int parseSpectraRange(const std::string &spec_range, int &spec_from, int &spec_to)
int sendSMS(const std::string &phone, const std::string &message, DAEstatus &status)
Win32Exception(const std::string &message)
void findConsecutive(const std::vector< T > &vec, T invalid_val, std::vector< int > &indexes, std::vector< int > &sizes)
return an array of indices containing the start points of consecutive sequences of numbers ...
int atomic_write(const std::string &filename, const std::string &old_suffix, const std::string &new_suffix, T *data, int n)
CRTLException(const std::string &message)
LARGE_INTEGER m_perf_counter
static int flushed_write(const std::vector< std::string > &files, const std::vector< const void * > &data, const std::vector< int > &nbytes)
GenericTask(const std::string &name, Poco::SharedPtr< Poco::Event > finish_event, const func_t &func)
void printComputerMemoryStats(DAEstatus &status)
std::string convertLatin1ToUTF8(const std::string &latin_string)
convert ISO Latin-1 (8859-1) to UTF-8
int64_t fileSizeBytes(const std::string &filename)