src.ctf.fs: honor component's initial log level
[babeltrace.git] / src / plugins / ctf / fs-src / file.c
CommitLineData
e98a2d6e
PP
1/*
2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
98903a3e
PP
23#define BT_LOG_OUTPUT_LEVEL (file->log_level)
24#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE"
25#include "logging/log.h"
26
e98a2d6e
PP
27#include <stdio.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <glib.h>
56a1cced 32#include "file.h"
e98a2d6e 33
e7a4393b 34BT_HIDDEN
e98a2d6e
PP
35void ctf_fs_file_destroy(struct ctf_fs_file *file)
36{
e98a2d6e
PP
37 if (!file) {
38 return;
39 }
40
41 if (file->fp) {
55314f2a 42 BT_LOGD("Closing file \"%s\" (%p)",
dcbaa1ca 43 file->path ? file->path->str : NULL, file->fp);
e98a2d6e
PP
44
45 if (fclose(file->fp)) {
55314f2a 46 BT_LOGE("Cannot close file \"%s\": %s",
dcbaa1ca
JG
47 file->path ? file->path->str : "NULL",
48 strerror(errno));
e98a2d6e
PP
49 }
50 }
51
52 if (file->path) {
53 g_string_free(file->path, TRUE);
54 }
55
56 g_free(file);
57}
58
e7a4393b 59BT_HIDDEN
98903a3e 60struct ctf_fs_file *ctf_fs_file_create(bt_logging_level log_level)
e98a2d6e
PP
61{
62 struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1);
63
64 if (!file) {
65 goto error;
66 }
67
98903a3e 68 file->log_level = log_level;
e98a2d6e
PP
69 file->path = g_string_new(NULL);
70 if (!file->path) {
71 goto error;
72 }
73
74 goto end;
75
76error:
77 ctf_fs_file_destroy(file);
78 file = NULL;
79
80end:
81 return file;
82}
83
e7a4393b 84BT_HIDDEN
55314f2a 85int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
e98a2d6e
PP
86{
87 int ret = 0;
88 struct stat stat;
89
3f7d4d90 90 BT_LOGI("Opening file \"%s\" with mode \"%s\"", file->path->str, mode);
e98a2d6e
PP
91 file->fp = fopen(file->path->str, mode);
92 if (!file->fp) {
55314f2a 93 BT_LOGE("Cannot open file \"%s\" with mode \"%s\": %s",
e98a2d6e
PP
94 file->path->str, mode, strerror(errno));
95 goto error;
96 }
97
3f7d4d90 98 BT_LOGI("Opened file: %p", file->fp);
e98a2d6e
PP
99
100 if (fstat(fileno(file->fp), &stat)) {
e07ab7c8 101 BT_LOGE("Cannot get file information: %s", strerror(errno));
e98a2d6e
PP
102 goto error;
103 }
104
105 file->size = stat.st_size;
3f7d4d90 106 BT_LOGI("File is %jd bytes", (intmax_t) file->size);
e98a2d6e
PP
107 goto end;
108
109error:
110 ret = -1;
111
112 if (file->fp) {
113 if (fclose(file->fp)) {
55314f2a 114 BT_LOGE("Cannot close file \"%s\": %s", file->path->str,
e98a2d6e
PP
115 strerror(errno));
116 }
117 }
118
119end:
120 return ret;
121}
This page took 0.048941 seconds and 4 git commands to generate.