iPhone GUI for real-time log message display
My goal is to have a screen on my GUI dedicated to logging real-time messages generated by my internal components. A certain limit will be set on the log messages so that older messages are pruned.
I'm thinking about implementing using a UITextView
with a NSMutableString
to store the output. I would have to perform manual pruning somehow on the NSMutableString
object.开发者_StackOverflow Is a better way to implement this?
Until you measure and prove that your initial approach is too slow, uses too much memory or is otherwise a problem, I wouldn't worry a great deal about performance. Still, I don't think I'd take Yakub's suggestion and work through an intermediate file.
UITextView
sounds fine. I'd probably first try collecting log messages into an NSMutableArray
to make pruning easy, then concatenate these into a single NSString
joined by newlines, and assign that to your text view.
At the risk of over-engineering what could be a development-time debugging aid, you might consider wrapping the management of the log messages (adds, prunes and generation of the NSString
) in a class. You'll have more freedom to change the internal workings without thinking about the code that uses it.
@interface LogManager : NSObject
{
NSMutableArray *messages;
}
- (void) addMessage:(NSString *)message;
- (NSString *) logAsString;
@end
Implementation is straightforward.
Note that you could also make this class aware of your UITextView
, but if you do, remember that updates to the user interface must be done on the main thread (see performSelectorOnMainThread:withObject:waitUntilDone:
.
If you manage your logs into multiple array records, I suggest implementing it as table with NSMutableArray.
Array performance is far better than manipulating NSMutableString.
I think you have to use NSFileManager class, take the help of NSFileManager class.
using this class you can perform your task well.
Step :
write your NSLog strings in NSMutableString by appending new logs generated at the end of preveous resultant string.
and write it to some temp txt file.
and read that file and display the content of that file in UITextView.
精彩评论