ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Write log to usb file Flow

2021-10-15 10:07:07  阅读:132  来源: 互联网

标签:char rw log Flow mtab Write path NULL usb


Write log to usb file Flow

//===================================================================================================================
//========================================= use for debug Start =====================================================
//===================================================================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
​
​
#define MAX_USB_PATH  (128)
​
void messageWrite2File(char *msg, int size);
void OMX_Logger_Print( const char *pcc8Function, const char *pcc8Format, ... );
​
int g_vewd_logmask = 0;
FILE *gLogMsgFile = NULL;
//FILE *gOutYUVFile = NULL;
​
static char outLogName[][MAX_USB_PATH] = {
    "",
    "/tmp/",
};
​
static int Detect_USBPath(char *path)
{
    FILE *fmtab;
    char *usb_path;
    char tmpBuf[128];
​
    memset((void*)path, 0, MAX_USB_PATH);
​
    /* lookup Linux's table */
    if ((fmtab = fopen("/etc/mtab", "r+"))!=NULL) {     
        while (fgets(tmpBuf, sizeof(tmpBuf), fmtab)) {
            if (strncmp(tmpBuf, "/dev/sd", 7) == 0) {
                char *pos = tmpBuf;
​
                strsep(&pos, " ");  //device path
                usb_path = strsep(&pos, " ");   //mount path
                if (usb_path) {
                    strncpy(path, usb_path, MAX_USB_PATH - 2);
                    strncat(path, "/", 1);
                    fclose(fmtab);   
                    return 0;
                }
            }
        }
        fclose(fmtab);
    }
​
    /* lookup Android's table */
    if ((fmtab = fopen("/proc/mounts", "r+"))!= NULL) {
        while (fgets(tmpBuf, sizeof(tmpBuf), fmtab)) {
            if (strncmp(tmpBuf, "/dev/fuse", 9) == 0 || strncmp(tmpBuf, "/dev/sd", 7) == 0) {
                char *pos = tmpBuf;
​
                strsep(&pos, " ");  //device path
                usb_path = strsep(&pos, " ");   //mount path
                if (usb_path) {
                    strncpy(path, usb_path, MAX_USB_PATH - 2);
                    strncat(path, "/", 1);
                    fclose(fmtab);
                    return 0;
                }
            }
        }
        fclose(fmtab);
    }
​
    return 1;
}
​
void vewd_dbg_enable()
{
    if(g_vewd_logmask == -3)
    {
        int idx=0;
        char fileName[128]={0};
        //struct timeval tv;
        //struct timezone tz;
        //gettimeofday (&tv , &tz);
​
        idx = Detect_USBPath(outLogName[0]);        
        sprintf(fileName , "%svewd-interface-log.txt",outLogName[idx]);
        
        gLogMsgFile = fopen(fileName,"w+");
    }
}
​
​
void vewd_interface_debug_init(void)
{
    //add for debug
    {
        FILE *fIn;
        char *debugMessage;
        char tmpValue[16] = {0};        
        char checkPath[4][20] = {"/tmp/OMXVDEC_DEBUG",
                                "/tmp/OMXV",
                                "/mtd_apexe/OMXV",
                                "/mnt/OMXV"};
        int   i, logMask=0;
    
        for(i = 0 ; i < 4 ; i ++)
        {
            fIn = fopen(checkPath[i],"r");
            if(fIn)
            {
                int readSize=0;
                readSize = fread(tmpValue, 1, 16, fIn);
                if(readSize != 0)
                {
                    logMask = atoi(tmpValue);
                }
                fclose(fIn);
            }
            if(logMask)
            {
                break;
            }
        }
   
        if(logMask != 0)
        {
            g_vewd_logmask = logMask;
            
        }
    }
​
    vewd_dbg_enable();
​
}
​
void vewd_dbg_disable()
{
    if(gLogMsgFile)
    {
        fclose(gLogMsgFile);
        gLogMsgFile = NULL;
    }
}
​
​
void messageWrite2File(char *msg, int size)
{
    if(gLogMsgFile)
    {
        struct timespec ts;
        clock_gettime( CLOCK_REALTIME, &ts);
        struct tm * timeinfo = localtime(&ts.tv_sec);
        static char headStr[100];
        if (NULL != timeinfo)
        {
            snprintf(headStr, 100, "%.2d:%.2d:%.2d.%.3ld ", timeinfo->tm_hour
                , timeinfo->tm_min, timeinfo->tm_sec, ts.tv_nsec / 1000000);
            fputs( headStr, gLogMsgFile );
        }
        fputs(msg, gLogMsgFile);
    }
}
​
//===================================================================================================================
//========================================= use for debug End  ======================================================
//===================================================================================================================
​

注释

/etc/mtab

fstab说明了我们需要挂载的文件系统,即在此声明的文件系统,我们的系统才能识别、挂载

mtab说明在我们的系统中当前实际挂载的文件系统,包括具体的属性(defaults默认实际挂载会显示属性为rw)

fstab 和 mtab 的区别

2007年10月27日 星期六 01:40

fstab 文件想必大家都很熟悉,记录了计算机上硬盘分区的相关信息,启动 Linux 的时候,检查分区的 fsck 命令,和挂载分区的 mount 命令,都需要 fstab 中的信息,来正确的检查和挂载硬盘。

除了 fstab 文件之外,还有一个 mtab 文件,和 fstab 文件一样在 /etc 文件下,位于 /etc/mtab ,这个文件又是干什么用的呢?

我们可以看一下 mtab 文件的内容 (这是我电脑上的 mtab 文件内容):

lrwxrwxrwx 1 root root 19 Sep 13 10:48 mtab -> ../proc/self/mounts

cat /etc/mtab

/dev/sda4 / reiserfs rw,noatime 0 0 proc /proc proc rw,nosuid,nodev,noexec 0 0 sysfs /sys sysfs rw,nosuid,nodev,noexec 0 0 udev /dev tmpfs rw,nosuid 0 0 devpts /dev/pts devpts rw,nosuid,noexec 0 0 /dev/sda2 /boot ext2 rw,noatime 0 0 shm /dev/shm tmpfs rw,noexec,nosuid,nodev 0 0 /dev/sda1 /mnt/winxp ntfs rw,utf8,umask=0 0 0 usbfs /proc/bus/usb usbfs rw,noexec,nosuid,devmode=0664,devgid=85 0 0 /dev/sdb5 /media/SWAP vfat rw,nosuid,nodev,shortname=lower,uid=1000 0 0 乍看上去,和 fstab 文件的结构和内容基本相同,但是不同的是,mtab 文件记录的是,当前已挂载的分区信息。 每当 mount 挂载分区、umount 卸载分区,都会动态更新 mtab,mtab 总是保持着当前系统中已挂载的分区信息,fdisk、df 这类程序,必须要读取 mtab 文件,才能获得当前系统中的分区挂载情况

/etc/fstab和/etc/mtab是什么关系?

具体说说看?-----/etc/fstab是个配置文件,

而/etc/mtab则显示的是已经被挂载的fs的清单文件 PS:个人观点

fgets

char *fgets(char *s, int size, FILE *stream);
//fgets()  reads  in  at  most one less than size characters from stream and stores them into the buffer pointed to by s.   Reading stops after an EOF or a newline.  If a newline is read, it is stored into the buffer.  A terminating null byte ('\0') is stored after the last character in the buffer.
fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read.

fgets 可以从stream 读取一行数据(最大:size -1 字节) s,若为:while( fgets()),则可以逐行读取整个file文件,遇到文件末尾返回值为NULL;

strsep

       #include <string.h>
​
       char *strsep(char **stringp, const char *delim);
       
// DESCRIPTION
       If  *stringp  is NULL, the strsep() function returns NULL and does nothing else.  Otherwise, this function finds the first token in the string *stringp, that is delimited by one of the bytes in the
       string delim.  This token is terminated by overwriting the delimiter with a null byte ('\0'), and *stringp is updated to point past the token.  In case no delimiter was found, the token is taken to
       be the entire string *stringp, and *stringp is made NULL.
​
RETURN VALUE
       The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.

解释:

strsep :分解字符串为一组字符串。从stringp指向的位置起向后扫描,遇到delim指向的字符串中的字符后,将此字符替换为NULL,返回stringp指向的地址。它适用于分割“关键字”在两个字符串之间只“严格出现一次”的情况。

返回值是分割关键字之前的字符串,stringp 表示分割后剩余的字符串。

For example:

#include <string>
​
#include <stdio.h>
​
 
​
int main(int arg, const char *argv[])
​
{
​
    char* string = strdup( "/home/yinlijun/project:/home/yinlijun:/home/someone");          /*字符串不能为常量,所以strdup*/
     char* p;
​
    while((p = strsep(&string, ":")) != NULL)        /*第一个参数设为二级指针, 字符串中所有的第二个参数(子串)最后会被替代成‘/0’*/ 
     {
         printf("%s/n", p);
     }
​
     return 0;
​
}
​
 
​
运行结果:
​
/home/yinlijun/project
​
/home/yinlijun
​
/home/someone
​

标签:char,rw,log,Flow,mtab,Write,path,NULL,usb
来源: https://blog.csdn.net/m0_37674443/article/details/120777577

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有