1 // 2 // ConfigurationManager.Fields.cs 3 // 4 // Author: 5 // Leonardo Taglialegne <leonardo.taglialegne@gmail.com> 6 // 7 // Copyright (c) 2013 Leonardo Taglialegne. 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 29 using System; 30 using Mono.WebServer.Log; 31 using Mono.WebServer.Options.Settings; 32 33 namespace Mono.WebServer.Options { 34 public abstract partial class ConfigurationManager { ConfigurationManager(string name)35 protected ConfigurationManager (string name) 36 { 37 settings = new SettingsCollection {help, version, verbose, printlog, 38 logFile, configFile, this.name, 39 loglevels}; 40 this.name.MaybeUpdate (SettingSource.Default, name); 41 } 42 43 #region Baking fields 44 readonly BoolSetting help = new BoolSetting ("help", "Shows this help message and exits.", prototype: "?|h|help"); 45 readonly BoolSetting version = new BoolSetting ("version", "Displays version information and exits.", "V|version"); 46 readonly BoolSetting verbose = new BoolSetting ("verbose", "Prints extra messages. Mainly useful for debugging.", prototype: "v|verbose"); 47 readonly BoolSetting printlog = new BoolSetting ("printlog", "Prints log messages to the console.", environment: "MONO_PRINTLOG|MONO_FCGI_PRINTLOG", defaultValue: true); 48 49 readonly StringSetting logFile = new StringSetting ("logfile", "Specifies a file to log events to.", "FastCgiLogFile", "MONO_LOGFILE|MONO_FCGI_LOGFILE"); 50 readonly StringSetting configFile = new StringSetting ("configfile|config-file", Descriptions.ConfigFile); 51 readonly StringSetting name = new StringSetting ("name", "Specifies a name to print in the log"); 52 53 readonly EnumSetting<LogLevel> loglevels = new EnumSetting<LogLevel> ("loglevels", Descriptions.LogLevels, "FastCgiLogLevels", "MONO_FCGI_LOGLEVELS", LogLevel.Standard); 54 #endregion 55 56 #region Typesafe properties 57 public bool Help { 58 get { return help; } 59 } 60 public bool Version { 61 get { return version; } 62 } 63 public bool Verbose { 64 get { return verbose; } 65 } 66 public bool PrintLog { 67 get { return printlog; } 68 } 69 70 public string LogFile { 71 get { return logFile; } 72 } 73 public string ConfigFile { 74 get { return configFile; } 75 } 76 public string Name { 77 get { return name; } 78 } 79 80 public LogLevel LogLevels { 81 get { return loglevels; } 82 } 83 #endregion 84 Add(params ISetting[] settings)85 protected void Add (params ISetting[] settings) 86 { 87 if (settings == null) 88 throw new ArgumentNullException ("settings"); 89 foreach (ISetting setting in settings) 90 this.settings.Add (setting); 91 } 92 SetupLogger()93 public void SetupLogger () 94 { 95 Logger.Level = LogLevels; 96 OpenLogFile (); 97 Logger.WriteToConsole = PrintLog; 98 Logger.Verbose = Verbose; 99 Logger.Name = Name; 100 } 101 OpenLogFile()102 void OpenLogFile () 103 { 104 try { 105 if (LogFile != null) 106 Logger.Open (LogFile); 107 } catch (Exception e) { 108 Logger.Write (LogLevel.Error, "Error opening log file: {0}", e.Message); 109 Logger.Write (LogLevel.Warning, "Events will not be logged to file."); 110 } 111 } 112 113 /// <summary> 114 /// If a configfile option was specified, tries to load 115 /// the configuration file 116 /// </summary> 117 /// <returns>false on failure, true on success or 118 /// option not present</returns> LoadConfigFile()119 public bool LoadConfigFile () 120 { 121 try { 122 if (ConfigFile != null) 123 if(!TryLoadXmlConfig (ConfigFile)) 124 return false; 125 } catch (ApplicationException e) { 126 Logger.Write (LogLevel.Error, e.Message); 127 return false; 128 } catch (System.Xml.XmlException e) { 129 Logger.Write (LogLevel.Error, 130 "Error reading XML configuration: {0}", 131 e.Message); 132 return false; 133 } 134 return true; 135 } 136 } 137 } 138