File: ErrLogImmediate.c

package info (click to toggle)
i2util 1.6-1
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 748 kB
  • ctags: 473
  • sloc: ansic: 5,290; perl: 1,151; makefile: 74; sh: 17
file content (149 lines) | stat: -rw-r--r-- 4,019 bytes parent folder | download
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
/*
 *      $Id$
 */
/************************************************************************
*									*
*			     Copyright (C)  2002			*
*				Internet2				*
*			     All Rights Reserved			*
*									*
************************************************************************/
/*
 *	File:		ErrLogImmediate.c
 *
 *	Author:		Jeff Boote
 *			Internet2
 *
 *	Date:		Tue Apr 23 10:27:23  2002
 *
 *	Description:	This file defines an "immediate-mode" logging
 *			function to be used as the `log_func' argument
 *			to the I2OpenErr() function.
 *
 *			Modified from code writen by John Clyne at UCAR...
 *
 *
 *		Based on code from UCAR DCS tools. Copyright information
 *		from UCAR follows:
 *
 *      Copyright 2012, University Corporation for Atmospheric Research.
 *
 *      This software may be used, subject to the terms of OSI's BSD-2 Clause
 *      License located at  http://www.opensource.org/licenses/bsd-license.php/
 */
#include <I2util/utilP.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*
 * Function:	I2ErrLogImmediate()
 *
 * Description:	The I2ErrLogImmediate() function is a client logging function
 *		that may be passed to I2OpenErr() as the `log_func' argument.
 *
 *		Upon invocation, I2ErrLogImmediate() writes a logging message
 *		via a call to fprintf(). The behavior of I2ErrLogImmediate()
 *		may be controlled by the structure pointed to by `arg'. The
 *		members of the structure pointed to by `arg' that may be
 *		set include:
 *
 *			*fp		A file pointer passed to be passed to 
 *					fprintf()
 *
 *			line_info	A bit mask indicating how each output
 *					line should be formatted. The mask
 *					is a bitwise inclusive OR of the
 *					valid attribute bits. If line_info
 *					is zero nothing is printed.
 *
 *			tformat		A format string as defined by
 *					strftime used to format the local
 *					time if the `line_info' attribute
 *					I2RTIME is set.
 *
 *		Valid attribute mask bits include:
 *
 *			I2NAME	
 *			I2FILE
 *			I2LINE
 *			I2DATE
 *			I2RTIME
 *			I2MSG
 *
 *		If I2NAME is set `ev->name', followed by a ":" is 
 *		copied to `arg->fp'.
 *
 *		If I2FILE is set the string "FILE=", followed by 
 *		`ev->file', followed by ",", followed by a space is copied 
 *		to `arg->fp'.
 *
 *		If I2LINE is set the string "LINE=", followed by the ascii
 *		representation of `ev->line', followed by ",", followed by 
 *		a space is copied to `arg->fp'.
 *
 *		If I2DATE is set the string "DATE=", followed by 
 *		`ev->date', followed by ",", followed by a space is copied 
 *		to `arg->fp'.
 *
 *		If I2RTIME is set the string "RTIME=", followed by 
 *		`time', followed by ",", followed by a space is copied 
 *		to `arg->fp', where `time' is string formatted by
 *		arg->tformat.
 *	
 *		If I2MSG is set `ev->msg' is copied to `arg->fp.
 *
 *		Finally, a trailing newline is copied to `arg->fp'
 *
 *
 * In Args:
 *
 *	*arg	A pointer to a I2LogImmediateAttr structure.
 *
 * Out Args:
 *
 * Return Values:
 *
 * Side Effects:
 */
void	I2ErrLogImmediate(
	struct I2ErrLogEvent	*ev,
	void			*arg,
	void			**data	__attribute__((unused))
        )
{
    I2LogImmediateAttr	*la = (I2LogImmediateAttr *) arg;
    FILE			*fp = la->fp;

    if(! fp) return;

    if(ev->mask & la->line_info & I2NAME)
        fprintf(fp, "%s: ", ev->name);
    if(ev->mask & la->line_info & I2FILE)
        fprintf(fp, "FILE=%s, ", ev->file);
    if(ev->mask & la->line_info & I2LINE)
        fprintf(fp, "LINE=%d, ", ev->line);
    if(ev->mask & la->line_info & I2DATE)
        fprintf(fp, "DATE=%s, ", ev->date);

    if(la->line_info & I2RTIME){
        time_t		curr;
        struct tm	*tm;
        char		ftime[64];

        time(&curr);
        tm = localtime(&curr);
        if( strftime(ftime,sizeof(ftime),la->tformat,tm))
            fprintf(fp, "RTIME=%s, ", ftime);
    }

    if(ev->mask & la->line_info & I2MSG)
        fprintf(fp, "%s", ev->msg);

    if(la->line_info && !(la->line_info & I2NONL))
        fprintf(fp, "\n");

    fflush(fp);
}