aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKristian Evers <kristianevers@gmail.com>2018-04-14 08:34:29 +0200
committerGitHub <noreply@github.com>2018-04-14 08:34:29 +0200
commitee22447de347c78944af849b10e0c9fd10184c28 (patch)
tree810fb2aec0f868825a504284feee8de57ca080fc /src
parent4378963e0046739890ef0e7da8641b5880db3beb (diff)
parent97a9bef9b6d78bcfc4cad49004aadc1c181369d5 (diff)
downloadPROJ-ee22447de347c78944af849b10e0c9fd10184c28.tar.gz
PROJ-ee22447de347c78944af849b10e0c9fd10184c28.zip
Merge pull request #924 from kbevers/logging_with_new_api
Logging with new api
Diffstat (limited to 'src')
-rw-r--r--src/cct.c98
-rw-r--r--src/pj_internal.c9
-rw-r--r--src/proj.h15
-rw-r--r--src/proj_internal.h16
4 files changed, 88 insertions, 50 deletions
diff --git a/src/cct.c b/src/cct.c
index 00fc2127..956f91b3 100644
--- a/src/cct.c
+++ b/src/cct.c
@@ -76,6 +76,7 @@ Thomas Knudsen, thokn@sdfe.dk, 2016-05-25/2017-10-26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdarg.h>
#include "proj.h"
#include "proj_internal.h"
@@ -86,13 +87,14 @@ Thomas Knudsen, thokn@sdfe.dk, 2016-05-25/2017-10-26
/* Prototypes for functions in proj_strtod.c */
double proj_strtod(const char *str, char **endptr);
double proj_atof(const char *str);
+static void logger(void *data, int level, const char *msg);
+static void print(PJ_LOG_LEVEL verbosity, const char *fmt, ...);
/* Prototypes from functions in this file */
char *column (char *buf, int n);
PJ_COORD parse_input_line (char *buf, int *columns, double fixed_height, double fixed_time);
-
static const char usage[] = {
"--------------------------------------------------------------------------------\n"
"Usage: %s [-options]... [+operator_specs]... infile...\n"
@@ -151,12 +153,57 @@ static const char usage[] = {
"--------------------------------------------------------------------------------\n"
};
+
+static void logger(void *data, int level, const char *msg) {
+ FILE *stream;
+ int log_tell = proj_log_level(PJ_DEFAULT_CTX, PJ_LOG_TELL);
+
+ stream = (FILE *) data;
+
+ /* if we use PJ_LOG_NONE we always want to print stuff to stream */
+ if (level == PJ_LOG_NONE) {
+ fprintf(stream, "%s", msg);
+ return;
+ }
+
+ /* should always print to stderr if level == PJ_LOG_ERROR */
+ if (level == PJ_LOG_ERROR) {
+ fprintf(stderr, "%s", msg);
+ return;
+ }
+
+ /* otherwise only print if log level set by user is high enough */
+ if (level <= log_tell)
+ fprintf(stream, "%s", msg);
+}
+
+FILE *fout;
+
+static void print(PJ_LOG_LEVEL log_level, const char *fmt, ...) {
+
+ va_list args;
+ char *msg_buf;
+
+ va_start( args, fmt );
+
+ msg_buf = (char *) malloc(100000);
+ if( msg_buf == NULL )
+ return;
+
+ vsprintf( msg_buf, fmt, args );
+
+ logger((void *) fout, log_level, msg_buf);
+
+ va_end( args );
+ free( msg_buf );
+}
+
+
int main(int argc, char **argv) {
PJ *P;
PJ_COORD point;
PJ_PROJ_INFO info;
OPTARGS *o;
- FILE *fout = stdout;
char *buf;
int nfields = 4, direction = 1, skip_lines = 0, verbose;
double fixed_z = HUGE_VAL, fixed_time = HUGE_VAL;
@@ -170,6 +217,8 @@ int main(int argc, char **argv) {
"s=skip-lines",
0};
+ fout = stdout;
+
o = opt_parse (argc, argv, "hvI", "cozts", longflags, longkeys);
if (0==o)
return 0;
@@ -179,26 +228,26 @@ int main(int argc, char **argv) {
return 0;
}
-
direction = opt_given (o, "I")? -1: 1;
- verbose = opt_given (o, "v");
+
+ verbose = MIN(opt_given (o, "v"), 3); /* log level can't be larger than 3 */
+ proj_log_level (PJ_DEFAULT_CTX, verbose);
+ proj_log_func (PJ_DEFAULT_CTX, (void *) fout, logger);
if (opt_given (o, "version")) {
- fprintf (stdout, "%s: %s\n", o->progname, pj_get_release ());
+ print (PJ_LOG_NONE, "%s: %s\n", o->progname, pj_get_release ());
return 0;
}
if (opt_given (o, "o"))
fout = fopen (opt_arg (o, "output"), "wt");
if (0==fout) {
- fprintf (stderr, "%s: Cannot open '%s' for output\n", o->progname, opt_arg (o, "output"));
+ print (PJ_LOG_ERROR, "%s: Cannot open '%s' for output\n", o->progname, opt_arg (o, "output"));
free (o);
return 1;
}
- if (verbose > 3)
- fprintf (fout, "%s: Running in very verbose mode\n", o->progname);
-
+ print (PJ_LOG_TRACE, "%s: Running in very verbose mode\n", o->progname);
if (opt_given (o, "z")) {
fixed_z = proj_atof (opt_arg (o, "z"));
@@ -218,7 +267,7 @@ int main(int argc, char **argv) {
/* cppcheck-suppress invalidscanf */
int ncols = sscanf (opt_arg (o, "c"), "%d,%d,%d,%d", columns_xyzt, columns_xyzt+1, columns_xyzt+2, columns_xyzt+3);
if (ncols != nfields) {
- fprintf (stderr, "%s: Too few input columns given: '%s'\n", o->progname, opt_arg (o, "c"));
+ print (PJ_LOG_ERROR, "%s: Too few input columns given: '%s'\n", o->progname, opt_arg (o, "c"));
free (o);
if (stdout != fout)
fclose (fout);
@@ -229,7 +278,7 @@ int main(int argc, char **argv) {
/* Setup transformation */
P = proj_create_argv (0, o->pargc, o->pargv);
if ((0==P) || (0==o->pargc)) {
- fprintf (stderr, "%s: Bad transformation arguments - (%s)\n '%s -h' for help\n",
+ print (PJ_LOG_ERROR, "%s: Bad transformation arguments - (%s)\n '%s -h' for help\n",
o->progname, pj_strerrno (proj_errno(P)), o->progname);
free (o);
if (stdout != fout)
@@ -237,15 +286,13 @@ int main(int argc, char **argv) {
return 1;
}
- if (verbose > 4) {
- info = proj_pj_info (P);
- fprintf (stdout, "Final: %s argc=%d pargc=%d\n", info.definition, argc, o->pargc);
- }
+ info = proj_pj_info (P);
+ print (PJ_LOG_TRACE, "Final: %s argc=%d pargc=%d\n", info.definition, argc, o->pargc);
if (direction==-1) {
/* fail if an inverse operation is not available */
- if (!proj_pj_info(P).has_inverse) {
- fprintf (stderr, "Inverse operation not available\n");
+ if (!info.has_inverse) {
+ print (PJ_LOG_ERROR, "Inverse operation not available\n");
if (stdout != fout)
fclose (fout);
return 1;
@@ -258,7 +305,7 @@ int main(int argc, char **argv) {
/* Allocate input buffer */
buf = calloc (1, 10000);
if (0==buf) {
- fprintf (stderr, "%s: Out of memory\n", o->progname);
+ print (PJ_LOG_ERROR, "%s: Out of memory\n", o->progname);
pj_free (P);
free (o);
if (stdout != fout)
@@ -274,7 +321,7 @@ int main(int argc, char **argv) {
char *c = column (buf, 1);
opt_eof_handler (o);
if (0==ret) {
- fprintf (stderr, "Read error in record %d\n", (int) o->record_index);
+ print (PJ_LOG_ERROR, "Read error in record %d\n", (int) o->record_index);
continue;
}
point = parse_input_line (buf, columns_xyzt, fixed_z, fixed_time);
@@ -291,9 +338,8 @@ int main(int argc, char **argv) {
if (HUGE_VAL==point.xyzt.x) {
/* otherwise, it must be a syntax error */
- fprintf (fout, "# Record %d UNREADABLE: %s", (int) o->record_index, buf);
- if (verbose)
- fprintf (stderr, "%s: Could not parse file '%s' line %d\n", o->progname, opt_filename (o), opt_record (o));
+ print (PJ_LOG_NONE, "# Record %d UNREADABLE: %s", (int) o->record_index, buf);
+ print (PJ_LOG_ERROR, "%s: Could not parse file '%s' line %d\n", o->progname, opt_filename (o), opt_record (o));
continue;
}
@@ -306,8 +352,8 @@ int main(int argc, char **argv) {
if (HUGE_VAL==point.xyzt.x) {
/* transformation error */
- fprintf (fout, "# Record %d TRANSFORMATION ERROR: %s (%s)",
- (int) o->record_index, buf, pj_strerrno (proj_errno(P)));
+ print (PJ_LOG_NONE, "# Record %d TRANSFORMATION ERROR: %s (%s)",
+ (int) o->record_index, buf, pj_strerrno (proj_errno(P)));
proj_errno_restore (P, err);
continue;
}
@@ -317,10 +363,10 @@ int main(int argc, char **argv) {
if (proj_angular_output (P, direction)) {
point.lpzt.lam = proj_todeg (point.lpzt.lam);
point.lpzt.phi = proj_todeg (point.lpzt.phi);
- fprintf (fout, "%14.10f %14.10f %12.4f %12.4f\n", point.xyzt.x, point.xyzt.y, point.xyzt.z, point.xyzt.t);
+ print (PJ_LOG_NONE, "%14.10f %14.10f %12.4f %12.4f\n", point.xyzt.x, point.xyzt.y, point.xyzt.z, point.xyzt.t);
}
else
- fprintf (fout, "%13.4f %13.4f %12.4f %12.4f\n", point.xyzt.x, point.xyzt.y, point.xyzt.z, point.xyzt.t);
+ print (PJ_LOG_NONE, "%13.4f %13.4f %12.4f %12.4f\n", point.xyzt.x, point.xyzt.y, point.xyzt.z, point.xyzt.t);
}
if (stdout != fout)
diff --git a/src/pj_internal.c b/src/pj_internal.c
index 4da47051..891e0b9f 100644
--- a/src/pj_internal.c
+++ b/src/pj_internal.c
@@ -364,11 +364,6 @@ to that context.
return;
}
-
-
-
-
-
/* logging */
/* pj_vlog resides in pj_log.c and relates to pj_log as vsprintf relates to sprintf */
@@ -376,11 +371,11 @@ void pj_vlog( projCtx ctx, int level, const char *fmt, va_list args );
/***************************************************************************************/
-enum proj_log_level proj_log_level (PJ_CONTEXT *ctx, enum proj_log_level log_level) {
+PJ_LOG_LEVEL proj_log_level (PJ_CONTEXT *ctx, PJ_LOG_LEVEL log_level) {
/****************************************************************************************
Set logging level 0-3. Higher number means more debug info. 0 turns it off
****************************************************************************************/
- enum proj_log_level previous;
+ PJ_LOG_LEVEL previous;
if (0==ctx)
ctx = pj_get_default_ctx();
if (0==ctx)
diff --git a/src/proj.h b/src/proj.h
index 7bc9b10e..3d64ed1a 100644
--- a/src/proj.h
+++ b/src/proj.h
@@ -268,6 +268,17 @@ struct PJ_INIT_INFO {
char lastupdate[16]; /* Date of last update in YYYY-MM-DD format */
};
+typedef enum PJ_LOG_LEVEL {
+ PJ_LOG_NONE = 0,
+ PJ_LOG_ERROR = 1,
+ PJ_LOG_DEBUG = 2,
+ PJ_LOG_TRACE = 3,
+ PJ_LOG_TELL = 4,
+ PJ_LOG_DEBUG_MAJOR = 2, /* for proj_api.h compatibility */
+ PJ_LOG_DEBUG_MINOR = 3 /* for proj_api.h compatibility */
+} PJ_LOG_LEVEL;
+
+typedef void (*PJ_LOG_FUNCTION)(void *, int, const char *);
/* The context type - properly namespaced synonym for projCtx */
@@ -342,7 +353,6 @@ double proj_xyz_dist (PJ_COORD a, PJ_COORD b);
PJ_COORD proj_geod (const PJ *P, PJ_COORD a, PJ_COORD b);
-
/* Set or read error level */
int proj_context_errno (PJ_CONTEXT *ctx);
int proj_errno (const PJ *P);
@@ -351,6 +361,9 @@ int proj_errno_reset (const PJ *P);
int proj_errno_restore (const PJ *P, int err);
const char* proj_errno_string (int err);
+PJ_LOG_LEVEL proj_log_level (PJ_CONTEXT *ctx, PJ_LOG_LEVEL level);
+void proj_log_func (PJ_CONTEXT *ctx, void *app_data, PJ_LOG_FUNCTION logf);
+
/* Scaling and angular distortion factors */
PJ_FACTORS proj_factors(PJ *P, PJ_COORD lp);
diff --git a/src/proj_internal.h b/src/proj_internal.h
index 3f6ccde0..1aca9201 100644
--- a/src/proj_internal.h
+++ b/src/proj_internal.h
@@ -97,25 +97,9 @@ double proj_vgrid_value(PJ *P, PJ_LP lp);
PJ_LP proj_hgrid_value(PJ *P, PJ_LP lp);
PJ_LP proj_hgrid_apply(PJ *P, PJ_LP lp, PJ_DIRECTION direction);
-/* High level functionality for handling thread contexts */
-enum proj_log_level {
- PJ_LOG_NONE = 0,
- PJ_LOG_ERROR = 1,
- PJ_LOG_DEBUG = 2,
- PJ_LOG_TRACE = 3,
- PJ_LOG_TELL = 4,
- PJ_LOG_DEBUG_MAJOR = 2, /* for proj_api.h compatibility */
- PJ_LOG_DEBUG_MINOR = 3 /* for proj_api.h compatibility */
-};
-
-/* Set logging level 0-3. Higher number means more debug info. 0 turns it off */
-enum proj_log_level proj_log_level (PJ_CONTEXT *ctx, enum proj_log_level log_level);
-typedef void (*PJ_LOG_FUNCTION)(void *, int, const char *);
-
void proj_log_error (PJ *P, const char *fmt, ...);
void proj_log_debug (PJ *P, const char *fmt, ...);
void proj_log_trace (PJ *P, const char *fmt, ...);
-void proj_log_func (PJ_CONTEXT *ctx, void *app_data, PJ_LOG_FUNCTION logf);
int pj_ellipsoid (PJ *);
void pj_inherit_ellipsoid_def (const PJ *src, PJ *dst);