LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 ea2b03a649f50c54e8ac0a77dbe77e3151712390
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  conf.c
 *    Description:  This is the .ini configure file parser example code.
 *
 *         Notice:  Default configure file: etc/example.conf
 *                  Default logger file   : /tmp/test.log
 *
 *        Version:  1.0.0(08/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
 
#include "iniparser.h"
#include "logger.h"
 
typedef struct log_conf_s
{
    char            file[128]; /* logger file */
    int             level;     /* logger level */
    int             size;      /* logger file maxsize, oversize will rollback */
} log_conf_t;
 
typedef struct sock_conf_s
{
    char            host[128];  /* socker server host name */
    int             port;       /* socket server listen port */
} sock_conf_t;
 
typedef struct conf_ctx_s
{
    log_conf_t       logger;
    sock_conf_t      sock;
} conf_ctx_t;
 
 
static void program_usage(char *progname)
{
    printf("Usage: %s [OPTION]...\n", progname);
    printf(" %s is an .ini parser example program.\n", progname);
 
    printf("\nMandatory arguments to long options are mandatory for short options too:\n");
    printf(" -c[conf    ]  Specify configure file\n");
    printf(" -d[debug   ]  Running in debug mode\n");
    printf(" -h[help    ]  Display this help information\n");
 
    printf("\n%s version v1.0.0\n", progname);
    return;
}
 
int parser_conf(const char *conf_file, conf_ctx_t *conf, int debug);
 
int main (int argc, char **argv)
{
    char               *conf_file="etc/example.conf";
    char               *progname=NULL;
    int                 debug = 0;
    int                 opt;
    conf_ctx_t          conf;
 
    struct option long_options[] = {
        {"conf", required_argument, NULL, 'c'},
        {"debug", no_argument, NULL, 'd'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
 
    progname = (char *)basename(argv[0]);
 
    /* parser the command line parameters */
    while ((opt = getopt_long(argc, argv, "c:dh", long_options, NULL)) != -1)
    {
        switch (opt)
        {
            case 'c': /* change default configure file */
                conf_file = optarg;
                break;
 
            case 'd': /* set debug running */
                debug = 1;
                break;
 
            case 'h': /* print help information */
                program_usage(progname);
                return 0;
 
            default:
                break;
        }
 
    }
 
    if( !conf_file )
        return 1;
 
    if( parser_conf(conf_file, &conf, debug) < 0 )
    {
        fprintf(stderr, "Parser configure file '%s' failure\n", conf_file);
        return 2;
    }
 
    log_warn("Parser configure file '%s' successfully\n", conf_file);
 
cleanup:
    log_close();
 
    return 0;
}
 
 
int parser_conf(const char *conf_file, conf_ctx_t *conf, int debug)
{
    dictionary         *ini;
    const char         *str;
    int                 val;
    int                 rv = 0;
    log_conf_t         *logger;
    sock_conf_t        *sock;
 
    if( !conf_file || !conf )
    {
        fprintf(stderr, "%s() Invalid input arguments\n", __func__);
        return -1;
    }
 
    memset(conf, 0, sizeof(*conf));
    logger = &conf->logger;
    sock = &conf->sock;
 
    ini = iniparser_load(conf_file);
    if( !ini )
    {
        fprintf(stderr, "ERROR: Configure file '%s' load failed\n", conf_file);
        return -2;
    }
 
    /*+------------------------------------------------------+
     *|    parser logger settings and start logger system    |
     *+------------------------------------------------------+*/
    if( !debug )
    {
        str = iniparser_getstring(ini, "logger:file", "iotd.log");
        strncpy(logger->file, str, sizeof(logger->file));
        logger->size = iniparser_getint(ini, "logger:size", 1024);
        logger->level = iniparser_getint(ini, "logger:level", LOG_LEVEL_INFO);
    }
    else
    {
        strncpy(logger->file, "console", sizeof(logger->file));
        logger->level = LOG_LEVEL_DEBUG;
        logger->size = 0;
    }
 
    if( log_open(logger->file, logger->level, logger->size, LOG_LOCK_DISABLE) < 0 )
    {
        fprintf(stderr, "Logger system initialise failure\n");
        rv = -3;
        goto cleanup;
    }
 
    /*+------------------------------------------------------+
     *|          parser socket server settings               |
     *+------------------------------------------------------+*/
 
    if( !(str=iniparser_getstring(ini, "server:hostname", NULL)) )
    {
        log_error("ERROR: Parser socket server hostname failure\n");
        rv = -4;
        goto cleanup;
    }
    strncpy(sock->host, str, sizeof(sock->host) );
 
    if( (val=iniparser_getint(ini, "server:port", -1)) < 0 )
    {
        log_error("ERROR: Parser socket server port failure\n");
        rv = -5;
        goto cleanup;
    }
    sock->port = val;
 
    log_info("Parser socket server [%s:%d]\n", sock->host, sock->port);
 
cleanup:
    if( ini )
        iniparser_freedict(ini);
 
    if( rv )
        log_close();
 
    return rv;
}