Ejemplo n.º 1
0
        static public void Initialize(string log_directory,
                                      string program_identifier,
                                      LogLevel cutoff_level,
                                      bool running_in_foreground)
        {
            Log.log_directory         = log_directory;
            Log.program_identifier    = program_identifier;
            Log.cutoff_level          = cutoff_level;
            Log.running_in_foreground = running_in_foreground;

            if (!running_in_foreground)
            {
                Console.WriteLine("beagrepd will run in the background.\nUse beagrep-status to check progress of beagrepd.\nFor log files check {0}/current-Beagrep.\n", Log.log_directory);
            }

            PruneOldLogs();

            log_name_prefix = String.Format("{0:yyyy-MM-dd-HH-mm-ss}-", DateTime.Now);

            if (program_identifier.Length > 6)
            {
                program_identifier_truncated = program_identifier.Substring(0, 6);
            }
            else
            {
                program_identifier_truncated = program_identifier;
            }

            log_writer       = NewLogWriter(program_identifier);
            exception_writer = NewDelayedLogWriter(program_identifier + "Exceptions");

            TextWriter console_log_writer;

            console_log_writer = NewDelayedLogWriter(program_identifier + "Console");

            TextWriter console_redirect_writer;

            if (running_in_foreground)
            {
                foreground_echo_writer  = Console.Out;
                console_redirect_writer = new TeeTextWriter(Console.Out, console_log_writer);
            }
            else
            {
                console_redirect_writer = console_log_writer;
            }

            // Redirect the console output to a special file
            Console.SetOut(console_redirect_writer);
            Console.SetError(console_redirect_writer);

            if (!running_in_foreground)
            {
                // Now redirect the *actual* stdout/stderr to our main
                // log file.  This is used to catch Mono crash reports
                // in our logs.  Note that this doesn't override the
                // console redirection above, which is good.
                StreamWriter sw = (StreamWriter)log_writer;
                FileStream   fs = (FileStream)sw.BaseStream;
                int          fd = (int)fs.Handle;
                Mono.Unix.Native.Syscall.dup2(fd, 1);                  // stdout
                Mono.Unix.Native.Syscall.dup2(fd, 2);                  // stderr

                // If we are running in the background, redirect stdin to /dev/null
                FileStream dev_null_stream = new FileStream("/dev/null",
                                                            FileMode.Open,
                                                            FileAccess.Read,
                                                            FileShare.ReadWrite);
                TextReader dev_null_reader = new StreamReader(dev_null_stream);
                Console.SetIn(dev_null_reader);
            }
        }
Ejemplo n.º 2
0
		static public void Initialize (string   log_directory,
					       string   program_identifier,
					       LogLevel cutoff_level,
					       bool     running_in_foreground)
		{
			Log.log_directory = log_directory;
			Log.program_identifier = program_identifier;
			Log.cutoff_level = cutoff_level;
			Log.running_in_foreground = running_in_foreground;

			if (! running_in_foreground)
				Console.WriteLine ("beagrepd will run in the background.\nUse beagrep-status to check progress of beagrepd.\nFor log files check {0}/current-Beagrep.\n", Log.log_directory);

			PruneOldLogs ();

			log_name_prefix = String.Format ("{0:yyyy-MM-dd-HH-mm-ss}-", DateTime.Now);

			if (program_identifier.Length > 6)
				program_identifier_truncated = program_identifier.Substring (0, 6);
			else
				program_identifier_truncated = program_identifier;

			log_writer = NewLogWriter (program_identifier);
			exception_writer = NewDelayedLogWriter (program_identifier + "Exceptions");
			
			TextWriter console_log_writer;
			console_log_writer = NewDelayedLogWriter (program_identifier + "Console");

			TextWriter console_redirect_writer;
			if (running_in_foreground) {
				foreground_echo_writer = Console.Out;
				console_redirect_writer = new TeeTextWriter (Console.Out, console_log_writer);
			} else {
				console_redirect_writer = console_log_writer;
			}

			// Redirect the console output to a special file
			Console.SetOut (console_redirect_writer);
			Console.SetError (console_redirect_writer);

			if (! running_in_foreground) {
				// Now redirect the *actual* stdout/stderr to our main
				// log file.  This is used to catch Mono crash reports
				// in our logs.  Note that this doesn't override the
				// console redirection above, which is good.
				StreamWriter sw = (StreamWriter) log_writer;
				FileStream fs = (FileStream) sw.BaseStream;
				int fd = (int) fs.Handle;
				Mono.Unix.Native.Syscall.dup2 (fd, 1); // stdout
				Mono.Unix.Native.Syscall.dup2 (fd, 2); // stderr

				// If we are running in the background, redirect stdin to /dev/null
				FileStream dev_null_stream = new FileStream ("/dev/null",
									     FileMode.Open,
									     FileAccess.Read,
									     FileShare.ReadWrite);
				TextReader dev_null_reader = new StreamReader (dev_null_stream);
				Console.SetIn (dev_null_reader);
			}
		}