Logging Within your Application

Native SDK Development

When logging activity within your application, you have a couple of options.  You can create your own logging system that writes activity out to a file.  However this isn’t ideal because it means constant writes to flash memory, which slows your application down and wears out flash memory.

The better option is to make use of the slogger2 API.  It uses in memory logging, but also provides a file you can access later to gather log statements made by your application.  The log file for your application can be found in /tmp/slogger2/<progname>.<pid>.

Logging in a Cascades Application

If you are creating a Cascades application, it’s even easier.  In a Cascades application qDebug and console.log is redirected to use slogger2.

Logging from C++ in a Cascades application.

qDebug() << "This is what I want to log";

Logging from QML in a Cascades application.

console.log("The logs are rolling, rolling, rolling.");

Logging in a non-Cascades Application

If you are creating a non Cascades application (pure Qt, C or C++) you can use the slogger2 API directly.  The first step is to initialize slogger2.


extern char *__progname;

slog2_buffer_t buffer_handle[1];

void slog2init() {

slog2_buffer_set_config_t buffer_config =

{1, __progname, SLOG2_ERROR,

{{"ManagedTraceTest", 8}}};

// Register the Buffer Set

slog2_register( &buffer_config, buffer_handle, 0 );

slog2_set_default_buffer( buffer_handle[0]);

}

After initializing, you can create log entries like this:



slog2c( NULL, 0, SLOG2_ERROR, "my first log" );

Where to Find Your Log Files

Either method creates a file you can access later to view activity in your application.  It is useful to add a feature to your application that could email this file to your support team.  The log statements for your application are logged in /tmp/slogger2/<progname>.<pid>.  You can also view your application’s log file by executing this command for debug releases of your application:

slog2info -l <file>

Access to logs is more restrictive for release builds of an application. To access the log files for a release build, use the slog2_dump_logs_to_file method to dump the logs files to a text file. Here’s a code example that dumps the log files to a file called mySlogs.txt within the logs directory of the application’s sandbox.


QString theFile = QDir::current().absoluteFilePath("logs/");

theFile.append("mySlogs.txt");


FILE *file = fopen(theFile.toStdString().c_str(), "w");

slog2_dump_logs_to_file( file, 0 );

fclose( file );

About MSohm

Mark Sohm has been helping developers create applications for BlackBerry devices for over 10 years, going all the way back to the very first BlackBerry JDK on BlackBerry OS 3.6.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus