美文网首页区块链研习社
比特币源码研读6-程序入口函数解析(5)

比特币源码研读6-程序入口函数解析(5)

作者: Jacky_2c9f | 来源:发表于2017-11-05 21:52 被阅读0次

这一节将继续讲解入口函数AppInit 里面的参数处理日志初始化操作。

入口函数文件:bitcoind.cpp

1. 参数错误处理

// Error out when loose non-argument tokens are encountered on command line

for (int i = 1; i < argc; i++) {

if (!IsSwitchChar(argv[i][0])) {

fprintf(stderr, "Error: Command line contains unexpected token '%s', see bitcoind -h for a list of options.\n", argv[i]);

exit(EXIT_FAILURE);

}

}

这段代码的作用就是判断参数中是否包含其他非标准转换字符,比如“-”或者“/”, 如果是的话,则返回错误提示信息,并退出程序。这段相对来说容易理解,输错参数,程序无法处理,就返回错误信息。

2. 设置参数

// -server defaults to true for bitcoind but not for the GUI so do this here

SoftSetBoolArg("-server", true);

上面的注释:对于bitcoind来说默认是服务端,对于GUI(图形用户界面)则不然,即需要显式设置,所以这里多了这行设置代码。

函数SoftSetBoolArg定义于util.h,代码如下:

static inline bool SoftSetBoolArg(const std::string& strArg, bool fValue)

{

return gArgs.SoftSetBoolArg(strArg, fValue);

}

实现代码在util.cpp:

bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)

{

if (fValue)

return SoftSetArg(strArg, std::string("1"));

else

return SoftSetArg(strArg, std::string("0"));

}

由于传进来的参数fValue值为true,因此进入第一个子句,再看看函数SoftSetArg, 判断如果mapArgs中如果存在“-server”字符串,那么直接返回false,即无需设置该参数;否则则进入到ForceSetArg函数。

bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)

{

LOCK(cs_args);

if (mapArgs.count(strArg))

return false;

ForceSetArg(strArg, strValue);

return true;

}

ForceSetArg函数的作用是把“-server:1”这对key-value放到mapArgs映射里面,然后将mapMultiArgs里面该参数的记录清除掉。最后用push_back方法将其添加到mapMultiArgs最后一个元素的后面。

void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)

{

LOCK(cs_args);

mapArgs[strArg] = strValue;

mapMultiArgs[strArg].clear();

mapMultiArgs[strArg].push_back(strValue);

}

3. 初始化日志

InitLogging();

代码在init.cpp:

void InitLogging()

{

fPrintToConsole = GetBoolArg("-printtoconsole", false);

fLogTimestamps = GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);

fLogTimeMicros = GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);

fLogIPs = GetBoolArg("-logips", DEFAULT_LOGIPS);

LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

LogPrintf("Bitcoin version %s\n", FormatFullVersion());

}

GetBoolArg函数:

bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault)

{

LOCK(cs_args);

if (mapArgs.count(strArg))

return InterpretBool(mapArgs[strArg]);

return fDefault;

}

GetBoolArg:如果mapArgs中存在strArg参数,则返回InterpretBool函数值,InterpretBool函数的作用是将字符串转为布尔值。

/** Interpret string as boolean, for argument parsing */

static bool InterpretBool(const std::string& strValue)

{

if (strValue.empty())

return true;

return (atoi(strValue) != 0);

}

 tips: atoi函数可以将字符串转化为整数。比如字符串“1” 转为整数1,字符串”3.1415“则转为整数3, 假如无法转换,则返回0, 如字符串”work 13".

逐个分析:

(1) fPrintToConsole

bool fPrintToConsole = false;

看定义,默认是false,即不输出日志到控制台。反之,则输出到控制台。

(2) fLogTimestamps

默认值DEFAULT_LOGTIMESTAMPS:true,即日志默认是打印时间戳的。

(3)fLogTimeMicros

micros: microsecond,微秒。默认是 DEFAULT_LOGTIMEMICROS:false, 即日志不按照微秒格式来记录。

(4)fLogIPs

默认是 DEFAULT_LOGIPS:false,即日志默认不记录IP地址。

这里留个小问题: 比特币默认的日志输出文件是哪个? 大家可以先摸索,下一节将给出答案,同时讲解参数的初始化设置。

作者:区块链研习社比特币源码研读班 Jacky

相关文章

网友评论

    本文标题:比特币源码研读6-程序入口函数解析(5)

    本文链接:https://www.haomeiwen.com/subject/zmefmxtx.html