/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

  1. // Copyright (C) 1999 Dominic Letourneau
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #ifndef _NETWORKLOGGER_CC_
  17. #define _NETWORKLOGGER_CC_
  18. #include "NetworkLogger.h"
  19. NetworkLogger::NetworkLogger (ostream &out) {
  20. output = &out;
  21. //let's obtain a semaphore
  22. if (sem_init (&semaphore, 1, 1) == -1) {
  23. cout<<"Unable to create the semaphore..."<<endl;
  24. }
  25. }
  26. NetworkLogger::~NetworkLogger () {
  27. sem_destroy (&semaphore);
  28. }
  29. void NetworkLogger::WriteLog(const string &data){
  30. obtainSemaphore();
  31. //critical section
  32. (*output)<<data;
  33. releaseSemaphore();
  34. }
  35. void NetworkLogger::WriteLog(BaseException *exception) {
  36. obtainSemaphore();
  37. //critical section
  38. exception->print(*output);
  39. releaseSemaphore();
  40. }
  41. void NetworkLogger::obtainSemaphore() {
  42. sem_wait(&semaphore);
  43. }
  44. void NetworkLogger::releaseSemaphore() {
  45. sem_post(&semaphore);
  46. }
  47. #endif