File: slow5_error.h

package info (click to toggle)
libslow5lib 0.7.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,084 kB
  • sloc: ansic: 11,825; python: 1,179; sh: 547; makefile: 90; cpp: 40
file content (163 lines) | stat: -rw-r--r-- 5,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
 * @file slow5_error.h
 * @brief SLOW5 error handling functions
 * @author Sasha Jenner (jenner.sasha@gmail.com), Hasindu Gamaarachchi (hasindu@garvan.org.au)
 * @date 27/02/2021
 */

/*
MIT License

Copyright (c) 2020 Sasha Jenner
Copyright (c) 2020 Hasindu Gamaarachchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


#ifndef SLOW5_ERROR_H
#define SLOW5_ERROR_H

#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif /* _cplusplus */

/* This is for internal use only - do not use any of the following directly*/

/* Debug and verbosity */

// the level of verbosity in the log printed to the standard error
enum slow5_log_level_opt {
    SLOW5_LOG_OFF,      // nothing at all
    SLOW5_LOG_ERR,      // error messages
    SLOW5_LOG_WARN,     // warning and error messages
    SLOW5_LOG_INFO,     // information, warning and error messages
    SLOW5_LOG_VERB,     // verbose, information, warning and error messages
    SLOW5_LOG_DBUG      // debugging, verbose, information, warning and error messages
};

// determines if the library exit behaviour (if exit(EXIT_FAILURE) is called) on error
enum slow5_exit_condition_opt {
    // do not exit the programme even if an error occur
    //the user should have done error handling for each function call - typical library behaviour in C  (default at the moment)
    SLOW5_EXIT_OFF,
    // exit the programme if an error occurs (useful for a programmer who is lazy to handle errors)
    SLOW5_EXIT_ON_ERR,
    // exit the programme if a warning occurs. this is a bit silly, but incase someone wants
    SLOW5_EXIT_ON_WARN
};

/* slow5 errno */
int *slow5_errno_location(void);
#define slow5_errno (*slow5_errno_location())

#define SLOW5_DEBUG_PREFIX "[DEBUG] %s: " /* TODO function before debug */
#define SLOW5_VERBOSE_PREFIX "[INFO] %s: "
#define SLOW5_INFO_PREFIX "[%s::INFO]\033[1;34m "
#define SLOW5_WARNING_PREFIX "[%s::WARNING]\033[1;33m "
#define SLOW5_ERROR_PREFIX "[%s::ERROR]\033[1;31m "
#define SLOW5_NO_COLOUR "\033[0m"

#define SLOW5_LOG_DEBUG(msg, ...) { \
    if (slow5_log_level >= SLOW5_LOG_DBUG) { \
        fprintf(stderr, SLOW5_DEBUG_PREFIX msg \
                " At %s:%d\n", \
                __func__, __VA_ARGS__, __FILE__, __LINE__ - 1); \
    } \
}

#define SLOW5_VERBOSE(msg, ...) { \
    if (slow5_log_level >= SLOW5_LOG_VERB) { \
        fprintf(stderr, SLOW5_VERBOSE_PREFIX msg "\n", __func__, __VA_ARGS__); \
    } \
}

#define SLOW5_INFO(msg, ...) { \
    if (slow5_log_level >= SLOW5_LOG_INFO) { \
        fprintf(stderr, SLOW5_INFO_PREFIX msg SLOW5_NO_COLOUR "\n", __func__, __VA_ARGS__); \
    } \
}

#define SLOW5_WARNING(msg, ...) { \
    if (slow5_log_level >= SLOW5_LOG_WARN) { \
        fprintf(stderr, SLOW5_WARNING_PREFIX msg SLOW5_NO_COLOUR \
                " At %s:%d\n", \
                __func__, __VA_ARGS__, __FILE__, __LINE__ - 1); \
    } \
    if (slow5_exit_condition >= SLOW5_EXIT_ON_WARN){ \
        SLOW5_INFO("%s", "Exiting on warning."); \
        exit(EXIT_FAILURE); \
    } \
}

#define SLOW5_ERROR(msg, ...) { \
    if (slow5_log_level >= SLOW5_LOG_ERR) { \
        fprintf(stderr, SLOW5_ERROR_PREFIX msg SLOW5_NO_COLOUR \
                " At %s:%d\n", \
                __func__, __VA_ARGS__, __FILE__, __LINE__ - 1); \
    } \
}

#define SLOW5_ERROR_EXIT(msg, ...) { \
    SLOW5_ERROR(msg, __VA_ARGS__) \
    SLOW5_EXIT_IF_ON_ERR() \
}

#define SLOW5_EXIT_IF_ON_ERR() { \
    if (slow5_exit_condition >= SLOW5_EXIT_ON_ERR){ \
        SLOW5_ERROR("%s", "Exiting on error."); \
        exit(EXIT_FAILURE); \
    } \
}

#define SLOW5_MALLOC_CHK(ret) { \
    if ((ret) == NULL) { \
        SLOW5_MALLOC_ERROR() \
    } \
}

#define SLOW5_MALLOC_CHK_EXIT(ret) { \
    if ((ret) == NULL) { \
        SLOW5_MALLOC_ERROR_EXIT() \
    } \
}

#define SLOW5_MALLOC_ERROR() SLOW5_ERROR("Failed to allocate memory: %s", strerror(errno))
#define SLOW5_MALLOC_ERROR_EXIT() SLOW5_ERROR_EXIT("Failed to allocate memory: %s", strerror(errno))

#define SLOW5_ASSERT(ret) { \
    if ((ret) == 0){ \
        fprintf(stderr, SLOW5_ERROR_PREFIX "Assertion failed." SLOW5_NO_COLOUR \
                " At %s:%d\nExiting.\n", \
                __func__ , __FILE__, __LINE__ - 1); \
        exit(EXIT_FAILURE); \
    } \
}

#ifdef __cplusplus
}
#endif /* _cplusplus */

#endif /* slow5_error.h */