PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ctaocrypt/src/logging.c

https://github.com/andersmalm/cyassl
C | 157 lines | 104 code | 31 blank | 22 comment | 9 complexity | 91fac1ba9e133359c4f6af20a38efc42 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* logging.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. /* submitted by eof */
  25. #include <cyassl/ctaocrypt/settings.h>
  26. #include <cyassl/ctaocrypt/logging.h>
  27. #include <cyassl/ctaocrypt/error.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. CYASSL_API int CyaSSL_Debugging_ON(void);
  32. CYASSL_API void CyaSSL_Debugging_OFF(void);
  33. #ifdef __cplusplus
  34. }
  35. #endif
  36. #ifdef DEBUG_CYASSL
  37. /* Set these to default values initially. */
  38. static CyaSSL_Logging_cb log_function = 0;
  39. static int loggingEnabled = 0;
  40. #endif /* DEBUG_CYASSL */
  41. int CyaSSL_SetLoggingCb(CyaSSL_Logging_cb f)
  42. {
  43. #ifdef DEBUG_CYASSL
  44. int res = 0;
  45. if (f)
  46. log_function = f;
  47. else
  48. res = BAD_FUNC_ARG;
  49. return res;
  50. #else
  51. (void)f;
  52. return NOT_COMPILED_IN;
  53. #endif
  54. }
  55. int CyaSSL_Debugging_ON(void)
  56. {
  57. #ifdef DEBUG_CYASSL
  58. loggingEnabled = 1;
  59. return 0;
  60. #else
  61. return NOT_COMPILED_IN;
  62. #endif
  63. }
  64. void CyaSSL_Debugging_OFF(void)
  65. {
  66. #ifdef DEBUG_CYASSL
  67. loggingEnabled = 0;
  68. #endif
  69. }
  70. #ifdef DEBUG_CYASSL
  71. #ifdef FREESCALE_MQX
  72. #include <fio.h>
  73. #else
  74. #include <stdio.h> /* for default printf stuff */
  75. #endif
  76. #ifdef THREADX
  77. int dc_log_printf(char*, ...);
  78. #endif
  79. static void cyassl_log(const int logLevel, const char *const logMessage)
  80. {
  81. if (log_function)
  82. log_function(logLevel, logMessage);
  83. else {
  84. if (loggingEnabled) {
  85. #ifdef THREADX
  86. dc_log_printf("%s\n", logMessage);
  87. #elif defined(MICRIUM)
  88. #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
  89. NetSecure_TraceOut((CPU_CHAR *)logMessage);
  90. #endif
  91. #else
  92. fprintf(stderr, "%s\n", logMessage);
  93. #endif
  94. }
  95. }
  96. }
  97. void CYASSL_MSG(const char* msg)
  98. {
  99. if (loggingEnabled)
  100. cyassl_log(INFO_LOG , msg);
  101. }
  102. void CYASSL_ENTER(const char* msg)
  103. {
  104. if (loggingEnabled) {
  105. char buffer[80];
  106. sprintf(buffer, "CyaSSL Entering %s", msg);
  107. cyassl_log(ENTER_LOG , buffer);
  108. }
  109. }
  110. void CYASSL_LEAVE(const char* msg, int ret)
  111. {
  112. if (loggingEnabled) {
  113. char buffer[80];
  114. sprintf(buffer, "CyaSSL Leaving %s, return %d", msg, ret);
  115. cyassl_log(LEAVE_LOG , buffer);
  116. }
  117. }
  118. void CYASSL_ERROR(int error)
  119. {
  120. if (loggingEnabled) {
  121. char buffer[80];
  122. sprintf(buffer, "CyaSSL error occured, error = %d", error);
  123. cyassl_log(ERROR_LOG , buffer);
  124. }
  125. }
  126. #endif /* DEBUG_CYASSL */