portother.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * FreeModbus Libary: Linux Port
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: portother.c,v 1.1 2006/08/01 20:58:49 wolti Exp $
  20. */
  21. /* ----------------------- Standard includes --------------------------------*/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <pthread.h>
  28. #include "port.h"
  29. /* ----------------------- Modbus includes ----------------------------------*/
  30. #include "mb.h"
  31. #include "mbport.h"
  32. #include "mbconfig.h"
  33. #include "mbctx.h"
  34. /* ----------------------- Defines ------------------------------------------*/
  35. #define NELEMS( x ) ( sizeof( ( x ) )/sizeof( ( x )[0] ) )
  36. /* ----------------------- Static variables ---------------------------------*/
  37. static FILE *fLogFile = NULL;
  38. static eMBPortLogLevel eLevelMax = MB_LOG_DEBUG;
  39. static pthread_mutex_t xLock = PTHREAD_MUTEX_INITIALIZER;
  40. /* ----------------------- Start implementation -----------------------------*/
  41. void
  42. vMBPortLogLevel( eMBPortLogLevel eNewLevelMax )
  43. {
  44. eLevelMax = eNewLevelMax;
  45. }
  46. void
  47. vMBPortLogFile( FILE * fNewLogFile )
  48. {
  49. fLogFile = fNewLogFile;
  50. }
  51. void
  52. vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... )
  53. {
  54. CHAR szBuf[512];
  55. int i;
  56. va_list args;
  57. FILE *fOutput = fLogFile == NULL ? stderr : fLogFile;
  58. static const char *arszLevel2Str[] = { "ERROR", "WARN", "INFO", "DEBUG" };
  59. i = snprintf( szBuf, NELEMS( szBuf ), "%s: %s: ", arszLevel2Str[eLevel], szModule );
  60. if( i != 0 )
  61. {
  62. va_start( args, szFmt );
  63. i += vsnprintf( &szBuf[i], NELEMS( szBuf ) - i, szFmt, args );
  64. va_end( args );
  65. }
  66. if( i != 0 )
  67. {
  68. if( eLevel <= eLevelMax )
  69. {
  70. //fputs( szBuf, fOutput );
  71. //log_dbg("%s",szBuf);
  72. }
  73. }
  74. }
  75. void
  76. vMBPortEnterCritical( fmodbus_t* ctx )
  77. {
  78. if( pthread_mutex_lock( &ctx->xLock ) != 0 )
  79. {
  80. vMBPortLog( MB_LOG_ERROR, "OTHER", "Locking primitive failed: %s\n", strerror( errno ) );
  81. }
  82. }
  83. void
  84. vMBPortExitCritical( fmodbus_t* ctx )
  85. {
  86. if( pthread_mutex_unlock( &ctx->xLock ) != 0 )
  87. {
  88. vMBPortLog( MB_LOG_ERROR, "OTHER", "Locking primitive failed: %s\n", strerror( errno ) );
  89. }
  90. }