Tizen 2.0 Release
[apps/core/preloaded/myfiles.git] / src / common / mf-log.c
1 /*
2  * Copyright 2013         Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19 #ifdef MYFILE_CRITICAL_LOG
20 #include <stdio.h>
21 #include <stdio.h>
22 #include <malloc.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdarg.h>
26
27 #include "mf-log.h"
28 #include "mf-dlog.h"
29 #include "mf-util.h"
30
31 static FILE *g_fp = NULL;
32
33 int mf_log_init()
34 {
35         pid_t process_id = 0;
36         process_id = getpid();
37
38         char *result_file = g_strdup_printf("%s-%d", MF_LOG_RESULT_FILE, (int)process_id);
39         if (result_file == NULL)
40                 return MYFILE_ERR_ALLOCATE_MEMORY_FAIL;
41
42         g_fp = fopen(result_file, "at+");
43
44         if(g_fp == NULL) {
45                 free(result_file);
46                 return MYFILE_ERR_FILE_OPEN_FAIL;
47         }
48
49         free(result_file);
50         return MYFILE_ERR_NONE;
51 }
52 void mf_log_finalize()
53 {
54         if (g_fp != NULL) {
55                 fclose(g_fp);
56                 g_fp = NULL;
57         }
58
59 }
60
61 int mf_log_record(char *filename, const char *function, int line, char *fmt, ...)
62 {
63         if (g_fp == NULL)
64                 return MYFILE_ERR_INVALID_ARG;
65
66         char *message = NULL;
67         va_list arg_ptr;
68         va_start(arg_ptr, fmt);
69         message = g_strdup_vprintf(fmt, arg_ptr);
70         va_end(arg_ptr);
71
72         fprintf(g_fp, MF_LOG_FORMAT, filename, function, line, message);
73         if (message != NULL) {
74                 free(message);
75                 message = NULL;
76         }
77
78         return MYFILE_ERR_NONE;
79 }
80 #endif