Coverage Report

Created: 2025-08-26 07:08

/src/PROJ/src/log.cpp
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 * Project:  PROJ.4
3
 * Purpose:  Implementation of pj_log() function.
4
 * Author:   Frank Warmerdam, warmerdam@pobox.com
5
 *
6
 ******************************************************************************
7
 * Copyright (c) 2010, Frank Warmerdam
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a
10
 * copy of this software and associated documentation files (the "Software"),
11
 * to deal in the Software without restriction, including without limitation
12
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
 * and/or sell copies of the Software, and to permit persons to whom the
14
 * Software is furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included
17
 * in all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
 * DEALINGS IN THE SOFTWARE.
26
 *****************************************************************************/
27
28
#include <stdarg.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
#include "proj.h"
34
#include "proj_internal.h"
35
36
/************************************************************************/
37
/*                          pj_stderr_logger()                          */
38
/************************************************************************/
39
40
void pj_stderr_logger(void *app_data, int level, const char *msg)
41
42
62.0k
{
43
62.0k
    (void)app_data;
44
62.0k
    (void)level;
45
62.0k
    fprintf(stderr, "%s\n", msg);
46
62.0k
}
47
48
/************************************************************************/
49
/*                           pj_log_active()                            */
50
/************************************************************************/
51
52
3.09M
bool pj_log_active(PJ_CONTEXT *ctx, int level) {
53
3.09M
    int debug_level = ctx->debug_level;
54
3.09M
    int shutup_unless_errno_set = debug_level < 0;
55
56
    /* For negative debug levels, we first start logging when errno is set */
57
3.09M
    if (ctx->last_errno == 0 && shutup_unless_errno_set)
58
0
        return false;
59
60
3.09M
    if (debug_level < 0)
61
0
        debug_level = -debug_level;
62
63
3.09M
    if (level > debug_level)
64
3.04M
        return false;
65
56.8k
    return true;
66
3.09M
}
67
68
/************************************************************************/
69
/*                               pj_vlog()                              */
70
/************************************************************************/
71
72
static void pj_vlog(PJ_CONTEXT *ctx, int level, const PJ *P, const char *fmt,
73
                    va_list args)
74
75
3.09M
{
76
3.09M
    char *msg_buf;
77
3.09M
    if (!pj_log_active(ctx, level))
78
3.04M
        return;
79
80
56.8k
    constexpr size_t BUF_SIZE = 100000;
81
56.8k
    msg_buf = (char *)malloc(BUF_SIZE);
82
56.8k
    if (msg_buf == nullptr)
83
0
        return;
84
85
56.8k
    if (P == nullptr || P->short_name == nullptr)
86
4.58k
        vsnprintf(msg_buf, BUF_SIZE, fmt, args);
87
52.2k
    else {
88
52.2k
        std::string fmt_with_P_short_name(P->short_name);
89
52.2k
        fmt_with_P_short_name += ": ";
90
52.2k
        fmt_with_P_short_name += fmt;
91
52.2k
        vsnprintf(msg_buf, BUF_SIZE, fmt_with_P_short_name.c_str(), args);
92
52.2k
    }
93
56.8k
    msg_buf[BUF_SIZE - 1] = '\0';
94
95
56.8k
    ctx->logger(ctx->logger_app_data, level, msg_buf);
96
97
56.8k
    free(msg_buf);
98
56.8k
}
99
100
/************************************************************************/
101
/*                               pj_log()                               */
102
/************************************************************************/
103
104
void pj_log(PJ_CONTEXT *ctx, int level, const char *fmt, ...)
105
106
119k
{
107
119k
    va_list args;
108
109
119k
    if (level > ctx->debug_level)
110
115k
        return;
111
112
4.55k
    va_start(args, fmt);
113
4.55k
    pj_vlog(ctx, level, nullptr, fmt, args);
114
4.55k
    va_end(args);
115
4.55k
}
116
117
/***************************************************************************************/
118
190k
PJ_LOG_LEVEL proj_log_level(PJ_CONTEXT *ctx, PJ_LOG_LEVEL log_level) {
119
    /****************************************************************************************
120
       Set logging level 0-3. Higher number means more debug info. 0 turns it
121
    off
122
    ****************************************************************************************/
123
190k
    PJ_LOG_LEVEL previous;
124
190k
    if (nullptr == ctx)
125
0
        ctx = pj_get_default_ctx();
126
190k
    if (nullptr == ctx)
127
0
        return PJ_LOG_TELL;
128
190k
    previous = static_cast<PJ_LOG_LEVEL>(abs(ctx->debug_level));
129
190k
    if (PJ_LOG_TELL == log_level)
130
156k
        return previous;
131
33.5k
    ctx->debug_level = log_level;
132
33.5k
    return previous;
133
190k
}
134
135
/*****************************************************************************/
136
52.3k
void proj_log_error(const PJ *P, const char *fmt, ...) {
137
    /******************************************************************************
138
       For reporting the most severe events.
139
    ******************************************************************************/
140
52.3k
    va_list args;
141
52.3k
    va_start(args, fmt);
142
52.3k
    pj_vlog(pj_get_ctx((PJ *)P), PJ_LOG_ERROR, P, fmt, args);
143
52.3k
    va_end(args);
144
52.3k
}
145
146
/*****************************************************************************/
147
0
void proj_log_debug(PJ *P, const char *fmt, ...) {
148
    /******************************************************************************
149
       For reporting debugging information.
150
    ******************************************************************************/
151
0
    va_list args;
152
0
    va_start(args, fmt);
153
0
    pj_vlog(pj_get_ctx(P), PJ_LOG_DEBUG, P, fmt, args);
154
0
    va_end(args);
155
0
}
156
157
/*****************************************************************************/
158
7.85k
void proj_context_log_debug(PJ_CONTEXT *ctx, const char *fmt, ...) {
159
    /******************************************************************************
160
       For reporting debugging information.
161
    ******************************************************************************/
162
7.85k
    va_list args;
163
7.85k
    va_start(args, fmt);
164
7.85k
    pj_vlog(ctx, PJ_LOG_DEBUG, nullptr, fmt, args);
165
7.85k
    va_end(args);
166
7.85k
}
167
168
/*****************************************************************************/
169
3.03M
void proj_log_trace(PJ *P, const char *fmt, ...) {
170
    /******************************************************************************
171
       For reporting embarrassingly detailed debugging information.
172
    ******************************************************************************/
173
3.03M
    va_list args;
174
3.03M
    va_start(args, fmt);
175
3.03M
    pj_vlog(pj_get_ctx(P), PJ_LOG_TRACE, P, fmt, args);
176
3.03M
    va_end(args);
177
3.03M
}
178
179
/*****************************************************************************/
180
16.7k
void proj_log_func(PJ_CONTEXT *ctx, void *app_data, PJ_LOG_FUNCTION logf) {
181
    /******************************************************************************
182
        Put a new logging function into P's context. The opaque object app_data
183
    is passed as first arg at each call to the logger
184
    ******************************************************************************/
185
16.7k
    if (nullptr == ctx)
186
0
        ctx = pj_get_default_ctx();
187
16.7k
    ctx->logger_app_data = app_data;
188
16.7k
    if (nullptr != logf)
189
16.7k
        ctx->logger = logf;
190
16.7k
}