read log messages

32 views
Skip to first unread message

owengerig

unread,
Sep 20, 2011, 12:04:07 PM9/20/11
to CocoaLumberjack
im trying to implement this library and have it working except for
reading back correct strings.
right now when i read the logmessage object from the database i get
this:

2011-09-20 10:59:26.925 Cameleon[5770:207] MSG Found <DDLogMessage:
0x4c2c290>

0x4c2c290 should be simple text (example: Cant connect, Connection
successful, etc...)

here is my implementation:
WRITE TO DB:

- (BOOL)db_log:(DDLogMessage *)logMessage
{
NSLog(@"trying to log message");
if (sqlite3_open([databasePath UTF8String], &CameleonLogDB) ==
SQLITE_OK)
{
char *errMsg;
NSString *insertSQL = [NSString stringWithFormat: @"INSERT
INTO LOG (MSG) VALUES (\"%@\")", logMessage];

const char *insert_stmt = [insertSQL UTF8String];

if (sqlite3_exec(CameleonLogDB, insert_stmt, NULL, NULL,
&errMsg) != SQLITE_OK)
{
NSLog(@"Failed to add log entry %s", errMsg);
}else{
NSLog(@"Log entry added");
}
sqlite3_close(CameleonLogDB);
return TRUE;
}else{
NSLog(@"Failed to add log entry due to: could not open db");
sqlite3_close(CameleonLogDB);
return FALSE;
}
}


READ FROM DB:
-(IBAction)updateLogButtonPressed
{

sqlite3 *CameleonLogDB;
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
NSString *databasePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"CameleonLogDB.db"]];
sqlite3_stmt *statement;

NSString *logMsg = [[NSString alloc]init];
DDLogMessage *logMessage;

NSLog(@"readlogs");

if (sqlite3_open([databasePath UTF8String], &CameleonLogDB) ==
SQLITE_OK)
{

NSLog(@"db opened");
const char *sqlStatement = "select * from LOG";
sqlite3_stmt *compiledStatement = nil;
//NSString *querySQL = [NSString stringWithFormat: @"SELECT *
FROM LOG"];

// const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(CameleonLogDB, sqlStatement, -1,
&compiledStatement, NULL) == SQLITE_OK)
{
NSLog(@"statement preped");

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
//DDLogMessage *logMsg = [logMsg stringByAppendingString:[NSString
stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
//logMsg = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 1)];
logMessage = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 1)];
//logMsg = logMessage->logMsg;

//logMsg = [NSString stringWithUTF8String:(char
*)sqlite3_column_text(compiledStatement, 1)]->logMsg;
NSLog(@"MSG Found %@",logMessage );

}
}else{
NSLog(@"No messages found");
}
sqlite3_finalize(compiledStatement);

sqlite3_close(CameleonLogDB);
}
return logMsg;

}

Joe Francia

unread,
Sep 20, 2011, 2:59:23 PM9/20/11
to cocoalu...@googlegroups.com
I'm not sure if your intention is to store the logMessage object or just the actual log message text.  If you just want the text, then you'll need to change this line:
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOG (MSG) VALUES (\"%@\")", logMessage];

to this:
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOG (MSG) VALUES (\"%@\")", logMessage->logMsg];

If you really need to store the log message object (and you most likely don't need to do this), you'll need to serialize it using NSKeyedArchiver to create an NSMutableData object that you can then store in a BLOB.  Just using the object as an argument to stringWithFormat will only give you the object's description, which defaults to <ClassName: obj_address> (unless you override -(NSString *)description).

You may also want to read up on implementing a custom logger: http://code.google.com/p/cocoalumberjack/wiki/CustomLoggers

And on a side note, there's a really nice Obj-C wrapper around the sqlite3 lib here: https://github.com/ccgus/fmdb

-- 
Joe Francia
Sent with Sparrow
--
You received this message because you are subscribed to the Google Groups "CocoaLumberjack" group.
To post to this group, send email to cocoalu...@googlegroups.com.
To unsubscribe from this group, send email to cocoalumberja...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cocoalumberjack?hl=en.

Robbie Hanson

unread,
Sep 20, 2011, 5:52:23 PM9/20/11
to cocoalu...@googlegroups.com
I know this may not answer your question, but the CocoaLumberjack project comes with a sample SQLiteLogger project that does something very similar. It may be quite useful.

-Robbie Hanson
Sent from my iPhone

owengerig

unread,
Sep 21, 2011, 9:05:35 AM9/21/11
to CocoaLumberjack
thank you this worked. i had tried this per the documentation but
must have done somthing wrong (i think i tried it on the output side
of things)
THANKS AGAIN

ill look into the sqlite wrapper i have read that using the straight
library is no suggested.

On Sep 20, 1:59 pm, Joe Francia <j...@joefrancia.com> wrote:
> I'm not sure if your intention is to store the logMessage object or just the actual log message text. If you just want the text, then you'll need to change this line:
> NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOG (MSG) VALUES (\"%@\")", logMessage];
>
> to this:
> NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO LOG (MSG) VALUES (\"%@\")", logMessage->logMsg];
>
> If you really need to store the log message object (and you most likely don't need to do this), you'll need to serialize it using NSKeyedArchiver to create an NSMutableData object that you can then store in a BLOB. Just using the object as an argument to stringWithFormat will only give you the object's description, which defaults to <ClassName: obj_address> (unless you override -(NSString *)description).
>
> You may also want to read up on implementing a custom logger:http://code.google.com/p/cocoalumberjack/wiki/CustomLoggers
>
> And on a side note, there's a really nice Obj-C wrapper around the sqlite3 lib here:https://github.com/ccgus/fmdb
>
> --
> Joe Francia
> Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
> > To post to this group, send email to cocoalu...@googlegroups.com (mailto:cocoalu...@googlegroups.com).
> > To unsubscribe from this group, send email to cocoalumberja...@googlegroups.com (mailto:cocoalumberja...@googlegroups.com).
Message has been deleted

owengerig

unread,
Sep 21, 2011, 9:09:34 AM9/21/11
to CocoaLumberjack
thank you
i was looking for an example and kept coming back to the site
never thought to look deeper into the zip file

On Sep 20, 4:52 pm, Robbie Hanson <robbiehan...@deusty.com> wrote:
> I know this may not answer your question, but the CocoaLumberjack project comes with a sample SQLiteLogger project that does something very similar. It may be quite useful.
>
> -Robbie Hanson
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages