Libtrap: Internal development docs  1.16.1
trap_internal.c
Go to the documentation of this file.
1 /**
2  * \file trap_internal.c
3  * \brief Internal functions and macros for libtrap
4  * Verbose and debug macros from libcommlbr
5  * \author Tomas Konir <Tomas.Konir@liberouter.org>
6  * \author Milan Kovacik <xkovaci1@fi.muni.cz>
7  * \author Vojtech Krmicek <xkrmicek@fi.muni.cz>
8  * \author Juraj Blaho <xblaho00@stud.fit.vutbr.cz>
9  * \author Tomas Cejka <cejkat@cesnet.cz>
10  * \date 2006-2011
11  * \date 2013
12  * \date 2014
13  *
14  * Copyright (C) 2006-2014 CESNET
15  *
16  *
17  * LICENSE TERMS
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  * notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  * 3. Neither the name of the Company nor the names of its contributors
29  * may be used to endorse or promote products derived from this
30  * software without specific prior written permission.
31  *
32  * ALTERNATIVELY, provided that this notice is retained in full, this
33  * product may be distributed under the terms of the GNU General Public
34  * License (GPL) version 2 or later, in which case the provisions
35  * of the GPL apply INSTEAD OF those given above.
36  *
37  * This software is provided ``as is'', and any express or implied
38  * warranties, including, but not limited to, the implied warranties of
39  * merchantability and fitness for a particular purpose are disclaimed.
40  * In no event shall the company or contributors be liable for any
41  * direct, indirect, incidental, special, exemplary, or consequential
42  * damages (including, but not limited to, procurement of substitute
43  * goods or services; loss of use, data, or profits; or business
44  * interruption) however caused and on any theory of liability, whether
45  * in contract, strict liability, or tort (including negligence or
46  * otherwise) arising in any way out of the use of this software, even
47  * if advised of the possibility of such damage.
48  *
49  */
50 #include <stdio.h>
51 #include "trap_internal.h"
52 
53 /**
54  * Verbose level storage
55  */
56 int trap_debug = 0;
57 int trap_verbose = -1;
58 char trap_err_msg[4096];
59 
60 /**
61  * \brief Return syslog and output level based on given verbose level
62  *
63  * \param level Verbose level
64  * \param str_level String that is set according to given verbose level
65  */
66 static void get_level(int level, char **str_level)
67 {
68  static char *error="ERROR";
69  static char *warning="WARNING";
70  static char *notice="NOTICE";
71  static char *verbose="VERBOSE";
72  static char *adv_verbose="ADVANCED VERBOSE";
73  static char *library="LIBRARY VERBOSE";
74 
75  switch (level) {
76  case CL_ERROR:
77  *str_level = error;
78  break;
79  case CL_WARNING:
80  *str_level= warning;
81  break;
82  case CL_VERBOSE_OFF:
83  *str_level= notice;
84  break;
85  case CL_VERBOSE_BASIC:
86  *str_level= verbose;
87  break;
89  *str_level= adv_verbose;
90  break;
91  case CL_VERBOSE_LIBRARY:
92  *str_level= library;
93  break;
94  default:
95  *str_level= notice;
96  }
97 }
98 
99 /**
100  * \brief send verbose message to stderr
101  *
102  * send verbose message to stderr. may change in future. don't use it directly
103  *
104  * \param level importance level
105  * \param string format string, like printf function
106  */
107 void trap_verbose_msg(int level, char *string)
108 {
109  char *strl;
110  get_level(level, &strl);
111  fprintf(stderr, "%s: %s\n", strl, string);
112  fflush(stderr);
113  string[0] = 0;
114 }
115 
116 #ifndef ATOMICOPS
117 static pthread_mutex_t atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
118 
119 _Bool __sync_bool_compare_and_swap_8(int64_t *ptr, int64_t oldvar, int64_t newval)
120 {
121  pthread_mutex_lock(&atomic_mutex);
122  int64_t tmp = *ptr;
123  *ptr = newval;
124  pthread_mutex_unlock(&atomic_mutex);
125  return tmp == oldvar;
126 }
127 
128 uint64_t __sync_fetch_and_add_8(uint64_t *ptr, uint64_t value)
129 {
130  pthread_mutex_lock(&atomic_mutex);
131  uint64_t tmp = *ptr;
132  *ptr += value;
133  pthread_mutex_unlock(&atomic_mutex);
134  return tmp;
135 }
136 
137 uint64_t __sync_add_and_fetch_8(uint64_t *ptr, uint64_t value)
138 {
139  pthread_mutex_lock(&atomic_mutex);
140  uint64_t tmp = *ptr;
141  *ptr += value;
142  pthread_mutex_unlock(&atomic_mutex);
143  return tmp;
144 }
145 
146 uint64_t __sync_and_and_fetch_8(uint64_t *ptr, uint64_t value)
147 {
148  pthread_mutex_lock(&atomic_mutex);
149  uint64_t tmp = *ptr;
150  *ptr &= value;
151  pthread_mutex_unlock(&atomic_mutex);
152  return tmp;
153 }
154 
155 uint64_t __sync_or_and_fetch_8(uint64_t *ptr, uint64_t value)
156 {
157  pthread_mutex_lock(&atomic_mutex);
158  uint64_t tmp = *ptr;
159  *ptr |= value;
160  pthread_mutex_unlock(&atomic_mutex);
161  return tmp;
162 }
163 
164 #endif
165 
_Bool __sync_bool_compare_and_swap_8(int64_t *ptr, int64_t oldvar, int64_t newval)
Internal functions and macros for libtrap Verbose and debug macros from libcommlbr.
int trap_debug
Definition: trap_internal.c:56
static pthread_mutex_t atomic_mutex
uint64_t __sync_and_and_fetch_8(uint64_t *ptr, uint64_t value)
char trap_err_msg[4096]
Definition: trap_internal.c:58
uint64_t __sync_add_and_fetch_8(uint64_t *ptr, uint64_t value)
uint64_t __sync_or_and_fetch_8(uint64_t *ptr, uint64_t value)
int trap_verbose
Definition: trap_internal.c:57
static void get_level(int level, char **str_level)
Return syslog and output level based on given verbose level.
Definition: trap_internal.c:66
uint64_t __sync_fetch_and_add_8(uint64_t *ptr, uint64_t value)
void trap_verbose_msg(int level, char *string)
send verbose message to stderr