gphoto2-port-log.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /** \file gphoto2-port-log.h
  2. *
  3. * Copyright 2001 Lutz Mueller <lutz@users.sf.net>
  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 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
  17. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301 USA
  19. */
  20. #ifndef __GPHOTO2_PORT_LOG_H__
  21. #define __GPHOTO2_PORT_LOG_H__
  22. #include <stdarg.h>
  23. /**
  24. * \brief Logging level
  25. * Specifies the logging severity level.
  26. */
  27. typedef enum {
  28. GP_LOG_ERROR = 0, /**< \brief Log message is an error infomation. */
  29. GP_LOG_VERBOSE = 1, /**< \brief Log message is an verbose debug infomation. */
  30. GP_LOG_DEBUG = 2, /**< \brief Log message is an debug infomation. */
  31. GP_LOG_DATA = 3 /**< \brief Log message is a data hex dump. */
  32. } GPLogLevel;
  33. /**
  34. * GP_LOG_ALL:
  35. *
  36. * Used by frontends if they want to be sure their
  37. * callback function receives all messages. Defined
  38. * as the highest debug level. Can make frontend code
  39. * more understandable and extension of log levels
  40. * easier.
  41. **/
  42. #define GP_LOG_ALL GP_LOG_DATA
  43. /**
  44. * \brief Logging function hook
  45. *
  46. * This is the function frontends can use to receive logging information
  47. * from the libgphoto2 framework. It is set using gp_log_add_func() and
  48. * removed using gp_log_remove_func() and will then receive the logging
  49. * messages of the level specified.
  50. *
  51. * \param level the log level of the passed message, as set by the camera driver or libgphoto2
  52. * \param domain the logging domain as set by the camera driver, or libgphoto2 function
  53. * \param str the logmessage, without linefeed
  54. * \param data the caller private data that was passed to gp_log_add_func()
  55. */
  56. typedef void (* GPLogFunc) (GPLogLevel level, const char *domain, const char *str, void *data);
  57. #ifndef DISABLE_DEBUGGING
  58. int gp_log_add_func (GPLogLevel level, GPLogFunc func, void *data);
  59. int gp_log_remove_func (int id);
  60. /* Logging */
  61. void gp_log (GPLogLevel level, const char *domain,
  62. const char *format, ...)
  63. #ifdef __GNUC__
  64. __attribute__((__format__(printf,3,4)))
  65. #endif
  66. ;
  67. void gp_log_with_source_location(
  68. GPLogLevel level, const char *file, int line, const char *func,
  69. const char *format, ...)
  70. #ifdef __GNUC__
  71. __attribute__((__format__(printf,5,6)))
  72. #endif
  73. ;
  74. void gp_logv (GPLogLevel level, const char *domain, const char *format,
  75. va_list args)
  76. #ifdef __GNUC__
  77. __attribute__((__format__(printf,3,0)))
  78. #endif
  79. ;
  80. void gp_log_data (const char *domain, const char *data, unsigned int size,
  81. const char *format, ...)
  82. #ifdef __GNUC__
  83. __attribute__((__format__(printf,4,5)))
  84. #endif
  85. ;
  86. /*
  87. * GP_DEBUG:
  88. * msg: message to log
  89. * params: params to message
  90. *
  91. * Logs message at log level #GP_LOG_DEBUG by calling #gp_log() with
  92. * an automatically generated domain
  93. * You have to define GP_MODULE as "mymod" for your module
  94. * mymod before using #GP_DEBUG().
  95. */
  96. #ifdef _GPHOTO2_INTERNAL_CODE
  97. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  98. #define GP_DEBUG(...) \
  99. gp_log(GP_LOG_DEBUG, GP_MODULE "/" __FILE__, __VA_ARGS__)
  100. /*
  101. * GP_LOG_D/E:
  102. * simple helper macros for convenient and consistent logging of error
  103. * and debug messages including information about the source location.
  104. */
  105. #define GP_LOG_D(...) gp_log(GP_LOG_DEBUG, __func__, __VA_ARGS__)
  106. #define GP_LOG_E(...) gp_log_with_source_location(GP_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
  107. #define GP_LOG_DATA(DATA, SIZE, MSG, ...) gp_log_data(__func__, DATA, SIZE, MSG, ##__VA_ARGS__)
  108. #elif defined(__GNUC__) && __GNUC__ >= 2
  109. #define GP_DEBUG(msg, params...) \
  110. gp_log(GP_LOG_DEBUG, GP_MODULE "/" __FILE__, msg, ##params)
  111. /*
  112. * GP_LOG_D/E:
  113. * simple helper macros for convenient and consistent logging of error
  114. * and debug messages including information about the source location.
  115. */
  116. #define GP_LOG_D(...) gp_log(GP_LOG_DEBUG, __func__, __VA_ARGS__)
  117. #define GP_LOG_E(...) gp_log_with_source_location(GP_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
  118. #define GP_LOG_DATA(DATA, SIZE, MSG, ...) gp_log_data(__func__, DATA, SIZE, MSG, ##__VA_ARGS__)
  119. #else
  120. # ifdef __GNUC__
  121. # warning Disabling GP_DEBUG because variadic macros are not allowed
  122. # endif
  123. #define GP_DEBUG (void)
  124. #define GP_LOG_D(...) /* no-op */
  125. #define GP_LOG_E(...) /* no-op */
  126. #define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
  127. #endif
  128. #endif /* _GPHOTO2_INTERNAL_CODE */
  129. #else /* DISABLE_DEBUGGING */
  130. /* Stub these functions out if debugging is disabled */
  131. #define gp_log_add_func(level, func, data) (0)
  132. #define gp_log_remove_func(id) (0)
  133. #define gp_log(level, domain, format, args...) /**/
  134. #define gp_log_with_source_location(level, file, line, func, format, ...)
  135. #define gp_logv(level, domain, format, args) /**/
  136. #define gp_log_data(domain, data, size) /**/
  137. #ifdef _GPHOTO2_INTERNAL_CODE
  138. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  139. #define GP_DEBUG(...) /* no-op */
  140. #define GP_LOG_D(...) /* no-op */
  141. #define GP_LOG_E(...) /* no-op */
  142. #define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
  143. #elif defined(__GNUC__)
  144. #define GP_DEBUG(msg, params...) /* no-op */
  145. #define GP_LOG_D(...) /* no-op */
  146. #define GP_LOG_E(...) /* no-op */
  147. #define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
  148. #else
  149. #define GP_DEBUG (void)
  150. #define GP_LOG_D (void /* no-op */
  151. #define GP_LOG_E (void) /* no-op */
  152. #define GP_LOG_DATA(void) /* no-op */
  153. #endif
  154. #endif /* _GPHOTO2_INTERNAL_CODE */
  155. #endif /* DISABLE_DEBUGGING */
  156. #ifdef _GPHOTO2_INTERNAL_CODE
  157. typedef struct StringFlagItem {
  158. char *str;
  159. unsigned int flag;
  160. } StringFlagItem;
  161. typedef void (*string_item_func) (const char *str, void *data);
  162. const char *
  163. gpi_enum_to_string(const unsigned int _enum,
  164. const StringFlagItem *map);
  165. int
  166. gpi_string_to_enum(const char *str,
  167. unsigned int *result,
  168. const StringFlagItem *map);
  169. void
  170. gpi_flags_to_string_list(const unsigned int flags,
  171. const StringFlagItem *map,
  172. string_item_func func, void *data);
  173. int
  174. gpi_string_or_to_flags(const char *str,
  175. unsigned int *flags,
  176. const StringFlagItem *map);
  177. unsigned int
  178. gpi_string_to_flag(const char *str,
  179. const StringFlagItem *map);
  180. unsigned int
  181. gpi_string_list_to_flags(const char *str[],
  182. const StringFlagItem *map);
  183. /* Allocates a sufficiently large buffer and interpolates the format
  184. * string with the proveded va_list args. The returned memory has to
  185. * be freed by the caller. */
  186. char*
  187. gpi_vsnprintf (const char* format, va_list args);
  188. #define C_MEM(MEM) do {\
  189. if ((MEM) == NULL) {\
  190. GP_LOG_E ("Out of memory: '%s' failed.", #MEM);\
  191. return GP_ERROR_NO_MEMORY;\
  192. }\
  193. } while(0)
  194. #define C_PARAMS(PARAMS) do {\
  195. if (!(PARAMS)) {\
  196. GP_LOG_E ("Invalid parameters: '%s' is NULL/FALSE.", #PARAMS);\
  197. return GP_ERROR_BAD_PARAMETERS;\
  198. }\
  199. } while(0)
  200. #define C_PARAMS_MSG(PARAMS, MSG, ...) do {\
  201. if (!(PARAMS)) {\
  202. GP_LOG_E ("Invalid parameters: " #MSG " ('%s' is NULL/FALSE.)", ##__VA_ARGS__, #PARAMS);\
  203. return GP_ERROR_BAD_PARAMETERS;\
  204. }\
  205. } while(0)
  206. #endif /* _GPHOTO2_INTERNAL_CODE */
  207. #endif /* __GPHOTO2_PORT_LOG_H__ */