blob: a53495b759b8b50ed862898a3acee344dcc6b33a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 MediaTek Inc.
4 */
5
6#include "util.h"
7#include <linux/fs.h>
8#include <linux/kernel.h>
9/* #include <asm/uaccess.h> */
10#include <linux/uaccess.h>
11
12#ifdef FILELOG
13
14static char tmp[1000] = { 0 };
15
16 /*TODO*/
17/**
18 * open file
19 * @param name path to open
20 * @return file pointer
21 */
22struct file *open_file(const char *name)
23{
24 struct file *fp = NULL;
25
26 fp = filp_open(name, O_WRONLY | O_APPEND /*| O_TRUNC */ | O_CREAT, 0664);
27 if (unlikely(fp == NULL)) {
28 pr_debug(KERNEL_INFO "can not open result file");
29 return NULL;
30 }
31 return fp;
32}
33
34/**
35 * write to file
36 * @param fp file pointer
37 * @param format format string
38 * @param ... variable-length subsequent arguments
39 */
40void write_file(struct file *fp, const char *format, ...)
41{
42 va_list va;
43 mm_segment_t fs = get_fs();
44
45 va_start(va, format);
46 vsnprintf(tmp, sizeof(tmp), format, va);
47 set_fs(KERNEL_DS);
48 vfs_write(fp, tmp, strlen(tmp), &(fp->f_pos));
49 set_fs(fs);
50 va_end(va);
51}
52
53/**
54 * close file
55 * @param fp file pointer
56 * @return exit code
57 */
58int close_file(struct file *fp)
59{
60 if (likely(fp != NULL)) {
61 filp_close(fp, NULL);
62 fp = NULL;
63 return 0;
64 }
65 pr_debug("cannot close file pointer:%p\n", fp);
66 return -1;
67}
68
69void filelog(char *str)
70{
71 struct file *fp;
72
73 fp = open_file("/data/met.log");
74 if (fp != NULL) {
75 write_file(fp, "%s", str);
76 close_file(fp);
77 }
78}
79
80#endif /* FILELOG */