blob: 051a3bd4f6c8834c2ebaf56cd29d673038b89abb [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2018 MediaTek Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include "util.h"
15#include <linux/fs.h>
16#include <linux/kernel.h>
17/* #include <asm/uaccess.h> */
18#include <linux/uaccess.h>
19
20#ifdef FILELOG
21
22static char tmp[1000] = { 0 };
23
24 /*TODO*/
25/**
26 * open file
27 * @param name path to open
28 * @return file pointer
29 */
30struct file *open_file(const char *name)
31{
32 struct file *fp = NULL;
33
34 fp = filp_open(name, O_WRONLY | O_APPEND /*| O_TRUNC */ | O_CREAT, 0664);
35 if (unlikely(fp == NULL)) {
36 pr_debug(KERNEL_INFO "can not open result file");
37 return NULL;
38 }
39 return fp;
40}
41
42/**
43 * write to file
44 * @param fp file pointer
45 * @param format format string
46 * @param ... variable-length subsequent arguments
47 */
48void write_file(struct file *fp, const char *format, ...)
49{
50 va_list va;
51 mm_segment_t fs = get_fs();
52
53 va_start(va, format);
54 vsnprintf(tmp, sizeof(tmp), format, va);
55 set_fs(KERNEL_DS);
56 vfs_write(fp, tmp, strlen(tmp), &(fp->f_pos));
57 set_fs(fs);
58 va_end(va);
59}
60
61/**
62 * close file
63 * @param fp file pointer
64 * @return exit code
65 */
66int close_file(struct file *fp)
67{
68 if (likely(fp != NULL)) {
69 filp_close(fp, NULL);
70 fp = NULL;
71 return 0;
72 }
73 pr_debug("cannot close file pointer:%p\n", fp);
74 return -1;
75}
76
77void filelog(char *str)
78{
79 struct file *fp;
80
81 fp = open_file("/data/met.log");
82 if (fp != NULL) {
83 write_file(fp, "%s", str);
84 close_file(fp);
85 }
86}
87
88#endif /* FILELOG */