xref: /minix3/common/dist/zlib/examples/gzlog.h (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: gzlog.h,v 1.1.1.1 2006/01/14 20:11:09 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /* gzlog.h
4*44bedb31SLionel Sambuc   Copyright (C) 2004 Mark Adler, all rights reserved
5*44bedb31SLionel Sambuc   version 1.0, 26 Nov 2004
6*44bedb31SLionel Sambuc 
7*44bedb31SLionel Sambuc   This software is provided 'as-is', without any express or implied
8*44bedb31SLionel Sambuc   warranty.  In no event will the author be held liable for any damages
9*44bedb31SLionel Sambuc   arising from the use of this software.
10*44bedb31SLionel Sambuc 
11*44bedb31SLionel Sambuc   Permission is granted to anyone to use this software for any purpose,
12*44bedb31SLionel Sambuc   including commercial applications, and to alter it and redistribute it
13*44bedb31SLionel Sambuc   freely, subject to the following restrictions:
14*44bedb31SLionel Sambuc 
15*44bedb31SLionel Sambuc   1. The origin of this software must not be misrepresented; you must not
16*44bedb31SLionel Sambuc      claim that you wrote the original software. If you use this software
17*44bedb31SLionel Sambuc      in a product, an acknowledgment in the product documentation would be
18*44bedb31SLionel Sambuc      appreciated but is not required.
19*44bedb31SLionel Sambuc   2. Altered source versions must be plainly marked as such, and must not be
20*44bedb31SLionel Sambuc      misrepresented as being the original software.
21*44bedb31SLionel Sambuc   3. This notice may not be removed or altered from any source distribution.
22*44bedb31SLionel Sambuc 
23*44bedb31SLionel Sambuc   Mark Adler    madler@alumni.caltech.edu
24*44bedb31SLionel Sambuc  */
25*44bedb31SLionel Sambuc 
26*44bedb31SLionel Sambuc /*
27*44bedb31SLionel Sambuc    The gzlog object allows writing short messages to a gzipped log file,
28*44bedb31SLionel Sambuc    opening the log file locked for small bursts, and then closing it.  The log
29*44bedb31SLionel Sambuc    object works by appending stored data to the gzip file until 1 MB has been
30*44bedb31SLionel Sambuc    accumulated.  At that time, the stored data is compressed, and replaces the
31*44bedb31SLionel Sambuc    uncompressed data in the file.  The log file is truncated to its new size at
32*44bedb31SLionel Sambuc    that time.  After closing, the log file is always valid gzip file that can
33*44bedb31SLionel Sambuc    decompressed to recover what was written.
34*44bedb31SLionel Sambuc 
35*44bedb31SLionel Sambuc    A gzip header "extra" field contains two file offsets for appending.  The
36*44bedb31SLionel Sambuc    first points to just after the last compressed data.  The second points to
37*44bedb31SLionel Sambuc    the last stored block in the deflate stream, which is empty.  All of the
38*44bedb31SLionel Sambuc    data between those pointers is uncompressed.
39*44bedb31SLionel Sambuc  */
40*44bedb31SLionel Sambuc 
41*44bedb31SLionel Sambuc /* Open a gzlog object, creating the log file if it does not exist.  Return
42*44bedb31SLionel Sambuc    NULL on error.  Note that gzlog_open() could take a long time to return if
43*44bedb31SLionel Sambuc    there is difficulty in locking the file. */
44*44bedb31SLionel Sambuc void *gzlog_open(char *path);
45*44bedb31SLionel Sambuc 
46*44bedb31SLionel Sambuc /* Write to a gzlog object.  Return non-zero on error.  This function will
47*44bedb31SLionel Sambuc    simply write data to the file uncompressed.  Compression of the data
48*44bedb31SLionel Sambuc    will not occur until gzlog_close() is called.  It is expected that
49*44bedb31SLionel Sambuc    gzlog_write() is used for a short message, and then gzlog_close() is
50*44bedb31SLionel Sambuc    called.  If a large amount of data is to be written, then the application
51*44bedb31SLionel Sambuc    should write no more than 1 MB at a time with gzlog_write() before
52*44bedb31SLionel Sambuc    calling gzlog_close() and then gzlog_open() again. */
53*44bedb31SLionel Sambuc int gzlog_write(void *log, char *data, size_t len);
54*44bedb31SLionel Sambuc 
55*44bedb31SLionel Sambuc /* Close a gzlog object.  Return non-zero on error.  The log file is locked
56*44bedb31SLionel Sambuc    until this function is called.  This function will compress stored data
57*44bedb31SLionel Sambuc    at the end of the gzip file if at least 1 MB has been accumulated.  Note
58*44bedb31SLionel Sambuc    that the file will not be a valid gzip file until this function completes.
59*44bedb31SLionel Sambuc  */
60*44bedb31SLionel Sambuc int gzlog_close(void *log);
61