WebServerIPhoneAppDelegate.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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:[DDOSLogger sharedInstance]];
  49. // We also want to direct our log messages to a file.
  50. // So we're going to setup file logging.
  51. //
  52. // We start by creating a file logger.
  53. fileLogger = [[DDFileLogger alloc] init];
  54. // Configure some sensible defaults for an iPhone application.
  55. //
  56. // Roll the file when it gets to be 512 KB or 24 Hours old (whichever comes first).
  57. //
  58. // Also, only keep up to 4 archived log files around at any given time.
  59. // We don't want to take up too much disk space.
  60. fileLogger.maximumFileSize = 1024 * 512; // 512 KB
  61. fileLogger.rollingFrequency = 60 * 60 * 24; // 24 Hours
  62. fileLogger.logFileManager.maximumNumberOfLogFiles = 4;
  63. // Add our file logger to the logging system.
  64. [DDLog addLogger:fileLogger];
  65. // Now setup our web server.
  66. //
  67. // This will allow us to connect to the device from our web browser.
  68. // We can then view log files, or view logging in real time as the application runs.
  69. [self setupWebServer];
  70. // This application, by itself, doesn't actually do anthing.
  71. // It is just a proof of concept or demonstration.
  72. // But we want to be able to see the application logging something.
  73. // So we setup a timer to spit out a silly log message.
  74. [NSTimer scheduledTimerWithTimeInterval:1.0
  75. target:self
  76. selector:@selector(writeLogMessages:)
  77. userInfo:nil
  78. repeats:YES];
  79. [window setRootViewController:viewController];
  80. [window makeKeyAndVisible];
  81. }
  82. - (void)writeLogMessages:(NSTimer *)aTimer
  83. {
  84. // Log a message in verbose mode.
  85. //
  86. // Want to disable this log message?
  87. // Try setting the log level (at the top of this file) to DDLogLevelWarning.
  88. // After doing this you can leave the log statement below.
  89. // It will automatically be compiled out (when compiling in release mode where compiler optimizations are enabled).
  90. DDLogVerbose(@"I like cheese");
  91. }
  92. @end