封装带线程名的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']