Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/error handling #2

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
fca7998
next
nikita-sakharin Jan 3, 2022
9e15e8d
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 6, 2022
2be91bd
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 8, 2022
08037c8
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 8, 2022
4a281fa
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 9, 2022
b42b614
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 10, 2022
2646763
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 13, 2022
e50f8cf
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 14, 2022
1008e18
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 18, 2022
c36e948
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 19, 2022
d63fdf4
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 28, 2022
a40906c
Merge branch 'master' into feature/error-handling
nikita-sakharin Jan 29, 2022
5dbaa66
next
nikita-sakharin Jan 29, 2022
a4f84a6
Merge branch 'master' into feature/error-handling
nikita-sakharin Feb 26, 2022
5f79288
Update error_handling.h
nikita-sakharin Apr 20, 2022
ba6582a
Merge branch 'master' into feature/error-handling
nikita-sakharin Apr 29, 2022
66d7007
Update error_handling.h
nikita-sakharin May 5, 2022
0c52787
next
nikita-sakharin May 12, 2022
843cb09
Merge branch 'master' into feature/error-handling
nikita-sakharin Jun 26, 2022
e179c51
Update error_handling.h
nikita-sakharin Jul 13, 2022
5f18dd1
printError function
nikita-sakharin Jul 20, 2022
67b6191
Merge branch 'master' into feature/error-handling
nikita-sakharin Jul 20, 2022
63e661b
Create error_handling.c
nikita-sakharin Jul 20, 2022
6bb125f
Update error_handling.h
nikita-sakharin Jul 20, 2022
0760853
include order
nikita-sakharin Jul 20, 2022
903d87c
static 1U for string
nikita-sakharin Jul 21, 2022
5b72cc2
ABORT_IF
nikita-sakharin Jul 25, 2022
a279992
argv0, error -> error, argv0
nikita-sakharin Jul 25, 2022
f06d102
Merge branch 'master' into feature/error-handling
nikita-sakharin Nov 6, 2022
17c8725
Update error_handling.h
nikita-sakharin Jan 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions include/c_utilities/error_handling.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
#ifndef C_UTILITIES_ERROR_HANDLING_H
#define C_UTILITIES_ERROR_HANDLING_H

#include <stdarg.h> // va_end, va_list, va_start
#include <stdbool.h> // false
#include <stdio.h> // fprintf, perror, stderr
#include <stdlib.h> // EXIT_FAILURE, exit
#include <stdio.h> // fflush, fprintf, perror, stderr, vfprintf
#include <stdlib.h> // EXIT_FAILURE, abort, exit
#include <string.h> // strerror

#define EXIT_IF(condition, message) \
#define ABORT_IF(condition, error) \
do { \
if ((condition)) { \
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \
perror((message)); \
fprintf(stderr, "%s:%d: %s: %s\n", \
__FILE__, __LINE__, __func__, strerror(error) \
); \
fflush(stderr); \
abort(); \
} \
} while (false)

#define EXIT_IF(condition, error, argv0, ...) \
do { \
if ((condition)) { \
printError(error, argv0, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} \
} while (false)

#ifdef NDEBUG
#define GOTO_IF(condition, label) \
do { \
if ((condition)) \
goto label; \
} while (false)
#else
#define GOTO_IF(condition, label) \
do { \
if ((condition)) { \
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); /* error ??? */ \
perror(__func__); \
goto label; \
} \
} while (false)
#endif // NDEBUG
// WRITE_ERRNO_GOTO_IF

inline void printError(
register const int error,
register const char argv0[const static 1U],
const char format[const static 1U],
...
) {
fprintf(stderr, "%s: ", argv0);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, ": %s\n", strerror(error));
}

#endif // C_UTILITIES_ERROR_HANDLING_H
3 changes: 3 additions & 0 deletions src/error_handling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <c_utilities/error_handling.h> // printError

extern inline void printError(int, const char [static 1U], const char [static 1U], ...);