File: except.c

package info (click to toggle)
sra-sdk 3.2.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296,076 kB
  • sloc: ansic: 532,876; cpp: 243,000; perl: 9,649; python: 8,978; sh: 7,888; java: 6,253; makefile: 1,148; yacc: 703; xml: 310; lex: 236
file content (232 lines) | stat: -rw-r--r-- 6,204 bytes parent folder | download | duplicates (6)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 */

#include "caps.h"
#include "ctx.h"
#include "except.h"

#include <klib/rc.h>
#include <klib/log.h>
#include <klib/status.h>
#include <klib/printf.h>

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "status.h"

FILE_ENTRY ( except );


/*--------------------------------------------------------------------------
 * exception-related activities
 */

static
rc_t log_msg ( const ctx_t *ctx, uint32_t lineno, KLogLevel lvl, const char *msg )
{
#if _DEBUGGING
    const ctx_info_t *ctx_info = ctx -> info;
    return pLogMsg ( lvl, "$(mod)/$(file).c:$(line): $(func): $(msg)",
                     "mod=%s,file=%s,line=%u,func=%s,msg=%s",
                     ctx_info -> mod,
                     ctx_info -> file,
                     lineno,
                     ctx_info -> func,
                     msg );
#else
    return LogMsg ( lvl, msg );
#endif
}

static
rc_t log_err ( const ctx_t *ctx, uint32_t lineno, KLogLevel lvl, rc_t rc, const char *msg )
{
#if _DEBUGGING
    const ctx_info_t *ctx_info = ctx -> info;
    return pLogErr ( lvl, rc, "$(mod)/$(file).c:$(line): $(func): $(msg)",
                     "mod=%s,file=%s,line=%u,func=%s,msg=%s",
                     ctx_info -> mod,
                     ctx_info -> file,
                     lineno,
                     ctx_info -> func,
                     msg );
#else
    return LogErr ( lvl, rc, msg );
#endif
}

static
rc_t status_msg ( const ctx_t *ctx, uint32_t lineno, uint32_t level, const char *msg )
{
#if _DEBUGGING
    const ctx_info_t *ctx_info = ctx -> info;
    return KStsMsg ( "%s/%s.c:%u: %s: [ %u ] %s",
                     ctx_info -> mod,
                     ctx_info -> file,
                     lineno,
                     ctx_info -> func,
                     level,
                     msg );
#else
    return KStsMsg ( "%s", msg );
#endif
}


/* Annotate
 *  make some annotation
 *  but not an error
 */
void Annotate ( const ctx_t *ctx, uint32_t lineno, KLogLevel level, const char *msg, ... )
{
    rc_t rc2;
    size_t num_writ;
    char buff [ 8 * 1024 ];

    va_list args;
    va_start ( args, msg );

    rc2 = string_vprintf ( buff, sizeof buff, & num_writ, msg, args );
    if ( rc2 != 0 )
        log_err ( ctx, lineno, klogInt, rc2, "Annotate failure" );
    else
        log_msg ( ctx, lineno, level, buff );

    va_end ( args );
}


/* Error
 *  make an annotation
 *  record an error as an rc_t
 */
void Error ( const ctx_t *ctx, uint32_t lineno, KLogLevel level, rc_t rc, const char *msg, ... )
{
    rc_t rc2;
    ctx_t *mctx;
    size_t num_writ;
    char buff [ 8 * 1024 ];

    va_list args;
    va_start ( args, msg );

    rc2 = string_vprintf ( buff, sizeof buff, & num_writ, msg, args );
    if ( rc2 != 0 )
        log_err ( ctx, lineno, klogInt, rc2, "Error annotation failure" );
    else
        log_err ( ctx, lineno, level, rc, buff );

    va_end ( args );

    for ( mctx = ( ctx_t* ) ctx; mctx != NULL && mctx -> rc == 0; mctx = ( ctx_t* ) mctx -> caller )
        mctx -> rc = rc;
}


/* Abort
 *  make an annotation
 *  record an error as an rc_t
 *  exit process
 */
void Abort ( const ctx_t *ctx, uint32_t lineno, rc_t rc, const char *msg, ... )
{
    rc_t rc2;
    ctx_t *mctx;
    size_t num_writ;
    char buff [ 8 * 1024 ];

    va_list args;
    va_start ( args, msg );

    rc2 = string_vprintf ( buff, sizeof buff, & num_writ, msg, args );
    if ( rc2 != 0 )
        log_err ( ctx, lineno, klogFatal, rc2, "Abort annotation failure" );
    else
        log_err ( ctx, lineno, klogFatal, rc, buff );

    va_end ( args );

    for ( mctx = ( ctx_t* ) ctx; mctx != NULL && mctx -> rc == 0; mctx = ( ctx_t* ) mctx -> caller )
        mctx -> rc = rc;

    exit ( -1 );
}


/* Clear
 *  clears annotation and error
 */
void ClearAnnotation ( const ctx_t *ctx )
{
    ClearError ( ctx );
}


/* ClearError
 *  clears error, leaving any annotation
 */
void ClearError ( const ctx_t *ctx )
{
    ctx_t *mctx;
    for ( mctx = ( ctx_t* ) ctx; mctx != NULL && mctx -> rc != 0; mctx = ( ctx_t* ) mctx -> caller )
        mctx -> rc = 0;
}


/* Status
 *  report status
 */
void Status ( const ctx_t *ctx, uint32_t lineno, uint32_t level, const char *msg, ... )
{
    if ( KStsLevelGet () >= level )
    {
        rc_t rc2;
        size_t num_writ;
        char buff [ 8 * 1024 ];

        va_list args;
        va_start ( args, msg );

        rc2 = string_vprintf ( buff, sizeof buff, & num_writ, msg, args );
        if ( rc2 != 0 )
            log_err ( ctx, lineno, klogInt, rc2, "Status failure" );
        else
            status_msg ( ctx, lineno, level, buff );

        va_end ( args );
    }
}

/* POLY_DISPATCH
 *  dispatch a polymorphic message
 */
void null_self_error ( const ctx_t *ctx, uint32_t lineno, const char *msg )
{
    rc_t rc = RC ( rcExe, rcFunction, rcResolving, rcSelf, rcNull );
    Error ( ctx, lineno, klogInt, rc, "cannot dispatch message '%s'", msg );
}