Libtrap: Internal development docs  1.16.1
trap_error.h
Go to the documentation of this file.
1 /**
2  * \file trap_error.h
3  * \brief Error handling for TRAP.
4  * \author Vaclav Bartos <ibartosv@fit.vutbr.cz>
5  * \author Tomas Cejka <cejkato2@fit.cvut.cz>
6  * \author Tomas Jansky <janskto1@fit.cvut.cz>
7  * \date 2013 - 2018
8  */
9 /*
10  * Copyright (C) 2013 - 2018 CESNET
11  *
12  * LICENSE TERMS
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in
21  * the documentation and/or other materials provided with the
22  * distribution.
23  * 3. Neither the name of the Company nor the names of its contributors
24  * may be used to endorse or promote products derived from this
25  * software without specific prior written permission.
26  *
27  * ALTERNATIVELY, provided that this notice is retained in full, this
28  * product may be distributed under the terms of the GNU General Public
29  * License (GPL) version 2 or later, in which case the provisions
30  * of the GPL apply INSTEAD OF those given above.
31  *
32  * This software is provided ``as is'', and any express or implied
33  * warranties, including, but not limited to, the implied warranties of
34  * merchantability and fitness for a particular purpose are disclaimed.
35  * In no event shall the company or contributors be liable for any
36  * direct, indirect, incidental, special, exemplary, or consequential
37  * damages (including, but not limited to, procurement of substitute
38  * goods or services; loss of use, data, or profits; or business
39  * interruption) however caused and on any theory of liability, whether
40  * in contract, strict liability, or tort (including negligence or
41  * otherwise) arising in any way out of the use of this software, even
42  * if advised of the possibility of such damage.
43  *
44  */
45 #ifndef _TRAP_ERROR_H_
46 #define _TRAP_ERROR_H_
47 
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 #include "trap_internal.h"
53 #include "../include/libtrap/trap.h"
54 
55 extern const char* default_err_msg[256]; // default messages
56 
57 /** Set error with default message.
58  *
59  * @param[in] ctx libtrap context
60  * @param[in] err_num Error number as defined in trap.h
61  * @return err_num
62  */
63 static inline int trap_error(trap_ctx_priv_t *ctx, int err_num)
64 {
65  if (ctx != NULL && ctx->trap_last_error != err_num) {
66  pthread_mutex_lock(&ctx->error_mtx);
67  ctx->trap_last_error = err_num;
68  if (err_num >= 0 &&
69  err_num < sizeof(default_err_msg)/sizeof(char*) &&
70  default_err_msg[err_num] != 0) {
71  ctx->trap_last_error_msg = default_err_msg[err_num];
72  }
73  else {
74  snprintf(ctx->error_msg_buffer, MAX_ERROR_MSG_BUFF_SIZE, "Unknown error (%i).", err_num);
76  }
77  pthread_mutex_unlock(&ctx->error_mtx);
78  }
79 
80  return err_num;
81 }
82 
83 
84 /** Set error with custom message (printf-like formatting).
85  *
86  * @param[in] ctx libtrap context
87  * @param[in] err_num Error number as defined in trap.h
88  * @param[in] msg Human-readable string describing error, supports printf formatting.
89  * @param[in] ... Additional parameters for printf-like formatting of msg.
90  * @return err_num
91  */
92 static inline int trap_errorf(trap_ctx_priv_t *ctx, int err_num, const char *msg, ...)
93 {
94  if (ctx != NULL) {
95  pthread_mutex_lock(&ctx->error_mtx);
96  ctx->trap_last_error = err_num;
97  va_list args;
98  va_start(args, msg);
99  vsnprintf(ctx->error_msg_buffer, MAX_ERROR_MSG_BUFF_SIZE, msg, args);
100  va_end(args);
102  pthread_mutex_unlock(&ctx->error_mtx);
103  }
104 
105  return err_num;
106 }
107 
108 /** Prepend given string before current ctx->trap_last_error_msg.
109  * This function is useful when a call of some function fails and you want
110  * to print a message about it but keep the original message about the error
111  * inside the function.
112  * Expected usage:
113  * return errorp("Call of myFunc failed: ");
114  *
115  * @param[in] ctx libtrap context
116  * @param[in] msg String to prepend current message
117  * @param[in] ... Additional parameters for printf-like formatting of msg.
118  * @return Current value of ctx->trap_last_error
119  */
120 static inline int trap_errorp(trap_ctx_priv_t *ctx, const char *msg, ...)
121 {
122  int retval = TRAP_E_BAD_FPARAMS;
123  if (ctx != NULL) {
124  pthread_mutex_lock(&ctx->error_mtx);
125  char tmp_str[MAX_ERROR_MSG_BUFF_SIZE];
126  strcpy(tmp_str, ctx->trap_last_error_msg);
127  va_list args;
128  va_start(args, msg);
129  int n = vsnprintf(ctx->error_msg_buffer, MAX_ERROR_MSG_BUFF_SIZE, msg, args);
130  va_end(args);
131  if (n >= 0 && n < MAX_ERROR_MSG_BUFF_SIZE) {
132  snprintf(ctx->error_msg_buffer + n, MAX_ERROR_MSG_BUFF_SIZE - n, tmp_str, args);
133  }
135  retval = ctx->trap_last_error;
136  pthread_mutex_unlock(&ctx->error_mtx);
137  }
138 
139  return retval;
140 }
141 
142 #endif
#define MAX_ERROR_MSG_BUFF_SIZE
Definition: trap_internal.h:57
Internal functions and macros for libtrap Verbose and debug macros from libcommlbr.
char error_msg_buffer[MAX_ERROR_MSG_BUFF_SIZE]
static int trap_errorp(trap_ctx_priv_t *ctx, const char *msg,...)
Definition: trap_error.h:120
static int trap_errorf(trap_ctx_priv_t *ctx, int err_num, const char *msg,...)
Definition: trap_error.h:92
#define TRAP_E_BAD_FPARAMS
Bad parameters of function.
Definition: trap.h:92
pthread_mutex_t error_mtx
const char * trap_last_error_msg
static int trap_error(trap_ctx_priv_t *ctx, int err_num)
Definition: trap_error.h:63
const char * default_err_msg[256]
Definition: trap_error.c:49