~ubuntu-branches/ubuntu/quantal/libee/quantal

« back to all changes in this revision

Viewing changes to src/ctx.c

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2010-12-11 12:37:09 UTC
  • Revision ID: james.westby@ubuntu.com-20101211123709-i8v7mpdtzhgjoqn5
Tags: upstream-0.1.0
ImportĀ upstreamĀ versionĀ 0.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Libee - An Event Expression Library inspired by CEE
 
2
 * Copyright 2010 by Rainer Gerhards and Adiscon GmbH.
 
3
 *
 
4
 * This file is part of libee.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 * A copy of the LGPL v2.1 can be found in the file "COPYING" in this distribution.
 
21
 */
 
22
#include "config.h"
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <stdarg.h>
 
26
 
 
27
#include "libee/libee.h"
 
28
#include "libee/internal.h"
 
29
#include "libee/ctx.h"
 
30
 
 
31
#define ERR_ABORT {r = 1; goto done; }
 
32
 
 
33
#define CHECK_CTX \
 
34
        if(ctx->objID != ObjID_CTX) { \
 
35
                r = -1; \
 
36
                goto done; \
 
37
        }
 
38
 
 
39
char *
 
40
ee_version(void)
 
41
{
 
42
        return VERSION;
 
43
}
 
44
 
 
45
 
 
46
ee_ctx
 
47
ee_initCtx(void)
 
48
{
 
49
        ee_ctx ctx;
 
50
        if((ctx = calloc(1, sizeof(struct ee_ctx_s))) == NULL)
 
51
                goto done;
 
52
 
 
53
        ctx->objID = ObjID_CTX;
 
54
        ctx->dbgCB = NULL;
 
55
        ctx->tagBucketSize = EE_DFLT_TAG_BCKT_SIZE;
 
56
        ctx->fieldBucketSize = EE_DFLT_FIELD_BCKT_SIZE;
 
57
done:
 
58
        return ctx;
 
59
}
 
60
 
 
61
 
 
62
int
 
63
ee_exitCtx(ee_ctx ctx)
 
64
{
 
65
        int r = 0;
 
66
 
 
67
        CHECK_CTX;
 
68
 
 
69
        ctx->objID = ObjID_None; /* prevent double free */
 
70
        free(ctx);
 
71
done:
 
72
        return r;
 
73
}
 
74
 
 
75
 
 
76
int
 
77
ee_setDebugCB(ee_ctx ctx, void (*cb)(void*, char*, size_t), void *cookie)
 
78
{
 
79
        int r = 0;
 
80
 
 
81
        CHECK_CTX;
 
82
        ctx->dbgCB = cb;
 
83
        ctx->dbgCookie = cookie;
 
84
done:
 
85
        return r;
 
86
}
 
87
 
 
88
/**
 
89
 * @internal
 
90
 * Generate some debug message and call the caller provided callback.
 
91
 *
 
92
 * Will first check if a user callback is registered. If not, returns
 
93
 * immediately.
 
94
 */
 
95
void
 
96
ee_dbgprintf(ee_ctx ctx, char *fmt, ...)
 
97
{
 
98
        va_list ap;
 
99
        char buf[8*1024];
 
100
        size_t lenBuf;
 
101
 
 
102
        if(ctx->dbgCB == NULL)
 
103
                goto done;
 
104
        
 
105
        va_start(ap, fmt);
 
106
        lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap);
 
107
        va_end(ap);
 
108
        if(lenBuf >= sizeof(buf)) {
 
109
                /* prevent buffer overrruns and garbagge display */
 
110
                buf[sizeof(buf) - 5] = '.';
 
111
                buf[sizeof(buf) - 4] = '.';
 
112
                buf[sizeof(buf) - 3] = '.';
 
113
                buf[sizeof(buf) - 2] = '\n';
 
114
                buf[sizeof(buf) - 1] = '\0';
 
115
                lenBuf = sizeof(buf) - 1;
 
116
        }
 
117
 
 
118
        ctx->dbgCB(ctx->dbgCookie, buf, lenBuf);
 
119
done: 
 
120
        return;
 
121
}
 
122