WebServerIPhoneAppDelegate.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // WebServerIPhoneAppDelegate.m
  3. // WebServerIPhone
  4. //
  5. // CocoaLumberjack Demos
  6. //
  7. #import "WebServerIPhoneAppDelegate.h"
  8. #import "WebServerIPhoneViewController.h"
  9. #import <CocoaLumberjack/CocoaLumberjack.h>
  10. #import "HTTPServer.h"
  11. #import "MyHTTPConnection.h"
  12. // Log levels: off, error, warn, info, verbose
  13. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  14. @implementation WebServerIPhoneAppDelegate
  15. @synthesize fileLogger;
  16. @synthesize window;
  17. @synthesize viewController;
  18. - (void)setupWebServer
  19. {
  20. // Create server using our custom MyHTTPServer class
  21. httpServer = [[HTTPServer alloc] init];
  22. // Configure it to use our connection class
  23. [httpServer setConnectionClass:[MyHTTPConnection class]];
  24. // Set the bonjour type of the http server.
  25. // This allows the server to broadcast itself via bonjour.
  26. // You can automatically discover the service in Safari's bonjour bookmarks section.
  27. [httpServer setType:@"_http._tcp."];
  28. // Normally there is no need to run our server on any specific port.
  29. // Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
  30. // However, for testing purposes, it may be much easier if the port doesn't change on every build-and-go.
  31. [httpServer setPort:12345];
  32. // Serve files from our embedded Web folder
  33. NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
  34. [httpServer setDocumentRoot:webPath];
  35. // Start the server (and check for problems)
  36. NSError *error = nil;
  37. if (![httpServer start:&error])
  38. {
  39. DDLogError(@"Error starting HTTP Server: %@", error);
  40. }
  41. }
  42. - (void)applicationDidFinishLaunching:(UIApplication *)application
  43. {
  44. // Direct log messages to the console.
  45. // The log messages will look exactly like a normal NSLog statement.
  46. //
  47. // This is something we may not want to do in a shipping version of the application.
  48. // [DDLog addLogger:[DDASLLogger sharedInstance]];
  49. [DDLog addLogger:[DDTTYLogger sharedInstance]];
  50. // We also want to direct our log messages to a file.
  51. // So we're going to setup file logging.
  52. //
  53. // We start by creating a file logger.
  54. fileLogger = [[DDFileLogger alloc] init];
  55. // Configure some sensible defaults for an iPhone application.
  56. //
  57. // Roll the file when it gets to be 512 KB or 24 Hours old (whichever comes first).
  58. //
  59. // Also, only keep up to 4 archived log files around at any given time.
  60. // We don't want to take up too much disk space.
  61. fileLogger.maximumFileSize = 1024 * 512; // 512 KB
  62. fileLogger.rollingFrequency = 60 * 60 * 24; // 24 Hours
  63. fileLogger.logFileManager.maximumNumberOfLogFiles = 4;
  64. // Add our file logger to the logging system.
  65. [DDLog addLogger:fileLogger];
  66. // Now setup our web server.
  67. //
  68. // This will allow us to connect to the device from our web browser.
  69. // We can then view log files, or view logging in real time as the application runs.
  70. [self setupWebServer];
  71. // This application, by itself, doesn't actually do anthing.
  72. // It is just a proof of concept or demonstration.
  73. // But we want to be able to see the application logging something.
  74. // So we setup a timer to spit out a silly log message.
  75. [NSTimer scheduledTimerWithTimeInterval:1.0
  76. target:self
  77. selector:@selector(writeLogMessages:)
  78. userInfo:nil
  79. repeats:YES];
  80. [window setRootViewController:viewController];
  81. [window makeKeyAndVisible];
  82. }
  83. - (void)writeLogMessages:(NSTimer *)aTimer
  84. {
  85. // Log a message in verbose mode.
  86. //
  87. // Want to disable this log message?
  88. // Try setting the log level (at the top of this file) to DDLogLevelWarning.
  89. // After doing this you can leave the log statement below.
  90. // It will automatically be compiled out (when compiling in release mode where compiler optimizations are enabled).
  91. DDLogVerbose(@"I like cheese");
  92. }
  93. @end