preface

Mars is an official cross-platform and cross-business terminal basic component of wechat. It is developed based on C++ language and can support Android, iOS, Mac and Windows platforms.

Contains four modules

  • Comm: a common library that can be used independently, including sockets, threads, message queues, coroutines, etc.

  • Xlog: a run-time log component with high reliability and performance.

  • SDT: Network diagnostic component;

  • STN: signaling distribution network module, which is the most important part of Mars.

This article introduces the xlog system for iOS integration with Mars.

The whole process of iOS access project

Clone Mars

If github SSH is not configured, you can choose HTTPS.


git clone [email protected]:Tencent/mars.git

Copy the code

Build Mars

Since Mars is written in C++, we need to compile Mars before importing it into iOS projects. I’m going to do it step by step.

  • Check whether cmake is installed

If not, install it


which cmake

/usr/local/bin/cmake

Copy the code

Brew is used to install Cmake. If brew is not installed, please install it by yourself.


brew install cmake

Copy the code
  • Compile the Mars

The Mars project has many py scripts compiled with one click, and here we use the ios script

// Go to the Mars folder CD./ Mars/Mars python build_ios.py // then select 2 Clean && build xlog and wait for compilationCopy the code

When you see the following copy both compiled successfully

==================Output========================

cmake_build/iOS/Darwin.out/mars.framework

Import the framework into the project

Will/Mars/Mars/cmake_build/iOS/Darwin out/Mars. The framework into iOS project

Import the following system Frameworks

  • Change appdelegate. m to appdelegate. mm compatible with c++

4. Write Helper methods, refer to Mars iOS Demo

JRXlogManager is a single example written based on the ios Demo Helper class in Mars

@interface JRXlogManager : NSObject + (instancetype)shared; - (void)initXlog:(const char *)prefixName pathName:(NSString *)pathName; /** closeXlog */ - (void)closeXlog; - (void)log:(XLoggerType)level tag:(const char *)tag content:(NSString *)content; - (void)infoLogWithTag:(const char *)tag Content:(NSString *)content; - (void)debugLogWithTag:(const char *)tag Content:(NSString *)content; - (void)errorLogWithTag:(const char *)tag Content:(NSString *)content; - (void)warningLogWithTag:(const char *)tag Content:(NSString *)content; @endCopy the code
  • Initialize the Xlog

So here I’m going to do it in app DidFinish, and I’m going to switch between login and demo.


[[JRXlogManager shared] closeXlog];

[[JRXlogManager shared] initXlog:[@"test_xlog_userId" UTF8String] pathName:XlogDirName];

[[JRXlogManager shared] infoLogWithTag:JRDebugMessage Content:@"App init()"];

Copy the code
  • Core method

Pub_key corresponds to a private_key. When decoding xlog files, a private_key is replaced in the script. Mars also provides the gen_key.py script to generate RSA keys. In the/Mars/Mars /log/crypt directory

- (void)initXlog:(const char *)prefixName pathName:(NSString *)pathName { NSString* logPath = getXlogPath(pathName); // set do not backup for logpath const char* attrName = "com.apple.MobileBackup"; u_int8_t attrValue = 1; setxattr([logPath UTF8String], attrName, &attrValue, sizeof(attrValue), 0, 0); // init xlog #if DEBUG xlogger_SetLevel(kLevelDebug); mars::xlog::appender_set_console_log(true); #else xlogger_SetLevel(kLevelInfo); appender_set_console_log(false); #endif mars::xlog::XLogConfig config; config.mode_ = mars::xlog::kAppenderAsync; config.logdir_ = [logPath UTF8String]; config.nameprefix_ = prefixName; config.compress_mode_ = mars::xlog::kZlib; config.compress_level_ = 0; config.cachedir_ = ""; config.cache_days_ = 0; // config.pub_key_ = ""; config.pub_key_ = "572d1e2710ae5fbca54c76a382fdd44050b3a675cb2bf39feebe85ef63d947aff0fa4943f1112e8b6af34bebebbaefa1a0aae055d9259b89a1858f7 cc9af9df1"; // PRIV_KEY = "145aa7717bf9745b91e9569b80bbf1eedaa6cc6cd0e26317d810e35710f44cf8" // PUB_KEY = "572d1e2710ae5fbca54c76a382fdd44050b3a675cb2bf39feebe85ef63d947aff0fa4943f1112e8b6af34bebebbaefa1a0aae055d9259b89a1858f7 cc9af9df1" appender_open(config); }Copy the code

5. Upload logs

Xlog has been saved to the sandbox path you set. Normally, you need to upload the sandbox path to your own server. You can choose to upload the user data periodically or forcibly (relying on socket push to the client). Of course, in the case of development mode, we can choose to upload by ourselves. By using apple’s own UIActivityViewController, we can easily use Air Drop to send to our MAC or copy to wechat. Specific code can see my following demo address source

Parse logs

Methods a

The Mars directory/Mars/Mars /log/crypt provides three scripts to parse xlog logs. However, if you use it directly, you should get an error. It has many module dependencies that you need to install with PIP.

, respectively,

  • decode_mars_crypt_log_file.py 

The private_key parameter needs to be replaced

  • decode_mars_nocrypt_log_file.py

This is unencrypted parsing. If pub_key in your ios project is null, that is, unencrypted, use the py script to parse.

  • gen_key.py

Generating a new pair of RSA does

Method 2

Use the XlogDecoder that I developed with Flutter Desktop to parse the Desktop. Currently Mac only, Windows will be supported later. Without any PY environment, just drag the XLog log, or folder, into the window. It will automatically parse the corresponding log for you. Encryption is supported with or without encryption, and you need to manually enter private_key for encryption

Vii. Attachment Address

  • xlog_ios_demo

  • xlog_decoder

At the end of

The xlog Docoder GUI I wrote later will also write corresponding articles, detailed explanation of the principle welcome Git Hub Star and gold digging.