/tags/rel-0-0-0/FreeSpeech/data-flow/src/NetworkLogger.cc
# · C++ · 58 lines · 29 code · 11 blank · 18 comment · 2 complexity · 86b4748e36081343c1ce35eb5b5ff166 MD5 · raw file
- // Copyright (C) 1999 Dominic Letourneau
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2, or (at your option)
- // any later version.
- //
- // This program is distributed in the hope that it will be useful, but
- // WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this file. If not, write to the Free Software Foundation,
- // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- #ifndef _NETWORKLOGGER_CC_
- #define _NETWORKLOGGER_CC_
- #include "NetworkLogger.h"
- NetworkLogger::NetworkLogger (ostream &out) {
- output = &out;
- //let's obtain a semaphore
- if (sem_init (&semaphore, 1, 1) == -1) {
- cout<<"Unable to create the semaphore..."<<endl;
- }
- }
- NetworkLogger::~NetworkLogger () {
- sem_destroy (&semaphore);
- }
- void NetworkLogger::WriteLog(const string &data){
- obtainSemaphore();
- //critical section
- (*output)<<data;
- releaseSemaphore();
- }
- void NetworkLogger::WriteLog(BaseException *exception) {
- obtainSemaphore();
- //critical section
- exception->print(*output);
- releaseSemaphore();
- }
- void NetworkLogger::obtainSemaphore() {
- sem_wait(&semaphore);
- }
- void NetworkLogger::releaseSemaphore() {
- sem_post(&semaphore);
- }
- #endif