logheader.h

154 篇文章 3 订阅
113 篇文章 0 订阅

封装带线程名的log 头文件

 


///
//personal debug switch// 
/********************************************************************************
P_DEBUG:
0     turn off debug 
1	    turn on debug 
 ********************************************************************************/
#ifndef P_LOGI 
#ifndef _PTHREAD_H_
#include <pthread.h>
//#define _PTHREAD_H_
#endif

#define BUF_SIZE 1024
char* getNameByPid(pid_t pid,pid_t tid) {
    char proc_pid_path[BUF_SIZE];
    char buf[BUF_SIZE];
    char *task_name = new char ;
    sprintf(proc_pid_path, "/proc/%d/task/%d/status", pid,tid);
    FILE* fp = fopen(proc_pid_path, "r");
    if(NULL != fp){
        if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
            fclose(fp);
        }
        fclose(fp);
        //sscanf(buf, "%*s%[^\n]%16[^&]", task_name);
        sscanf(buf, "%*s%[^\n]%*s", task_name);
        //sscanf(buf, "%*s%[^ \n]%*s", task_name);
        return task_name;
    }
    else{
        delete task_name;
        return (char*)"name-unknown";
    }
}

#include "utils/Log.h"
#define P_DEBUG	1
#if P_DEBUG
static char mark_label[64] = "pmain-nuwa";
char* argv[10]; 
#define SEPARATOR_LINE "----------------------------------"
      #define STA_LINE ">>>>>>>>>>>>>>>>>>>>>>>>>>>st.func"
      #define END_LINE "<<<<<<<<<<<<<<<<<<<<<<<<<<<ed.func"
#define LONNG_LINE = "------------------------------------------------------------------------------"
#define P_LOGI(fmt, ...) ALOGI("[%s ],tid:%u,%s,F:%s:%d,Fun:%s " fmt,getNameByPid( (unsigned int)getpid(), (unsigned int)gettid() ), (unsigned int)gettid(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__)
//#define P_LOGI(fmt, ...) ALOGI("tn:[%s],pid:%d,tid:%u,%s-,F:%s:%d, Fun:%s  " fmt,getNameByPid( (unsigned int)getpid(), (unsigned int)gettid() ), (unsigned int)getpid(), (unsigned int)gettid(),mark_label,(char*)__FILE__,__LINE__,(char*)__FUNCTION__,##__VA_ARGS__)
#else
#define P_LOGI(...)
#endif

#endif

///
///

static报错分割版,无论用static ,extern ,还是const,都会引起线程混乱,这是什么原因?加锁也无用,是我加错了吗?

 

 

 

处理多线程时会使无法开机等线程错乱,原因分析:

1.b2g中的和Nuwa的正常,且需要重新定义,而其他的在ipc下的不需要,是export.mozilla.ipc限制了吗?

2.废了一整个周末没解决!!!先放着,尴尬。。。

//logheader.h

#pragma once
#include <pthread.h>
#ifndef _LOGHEADER_H_
#define _LOGHEADER_H_ 
//namespace mozilla {
const char* getNameByPid(pid_t pid,pid_t tid);

//}
#endif

 

//logheader.cpp
#include <iostream>
#include <stdio.h>
#include <sys/file.h>
#include<stdlib.h>

#include "logheader.h"


#define BUF_SIZE 1024
//namespace mozilla {
//pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
//pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
using namespace std;
const char* getNameByPid(pid_t pid,pid_t tid) {
   
    char proc_pid_path[BUF_SIZE];
    char buf[BUF_SIZE];
    char *task_name = new char ;
    sprintf(proc_pid_path, "/proc/%d/task/%d/status", pid,tid);
    //sprintf(proc_pid_path, "/proc/%d/task/%d/stat", pid,tid);
    FILE* fp = fopen(proc_pid_path, "r");
    //int  fd = fileno(fp);
    //flock (fd, LOCK_EX);
    if(NULL != fp){
        if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
            fclose(fp);
        }
        fclose(fp);
        sscanf(buf, "%*s%[^\n]s", task_name);
        //sscanf(buf, "%*s%s", task_name);
        //sscanf(buf, "%*s%*[^(](%[^)]s", task_name);
    }
    else{
        task_name = (char*)"name-unknown";
    }


    //pthread_rwlock_unlock(&rwlock);
    //pthread_mutex_unlock(&counter_mutex);
    //flock (fd, LOCK_UN);
    free(fp);
    //delete task_name;
    return task_name;
}

嵌入模块,

#gecko/ipc/contentproc/moz.build

# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

Library('plugin-container')
if CONFIG['MOZ_B2G_LOADER']:
    FINAL_LIBRARY = 'xul'
EXPORTS.mozilla.ipc += [
        'logheader.h',
]

SOURCES += [
    'logheader.cpp',
    'plugin-container.cpp',
]

include('/ipc/chromium/chromium-config.mozbuild')

if CONFIG['OS_ARCH'] == 'WINNT':
    LOCAL_INCLUDES += [
        '/toolkit/xre',
        '/xpcom/base',
    ]


if CONFIG['MOZ_SANDBOX'] and CONFIG['OS_ARCH'] == 'WINNT':
    LOCAL_INCLUDES += [
        '/security/sandbox/chromium',
        '/security/sandbox/chromium-shim',
    ]

if CONFIG['GNU_CXX']:
    CXXFLAGS += ['-Wshadow']

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值