Google Api

This commit is contained in:
2017-06-20 02:47:52 +02:00
parent bddf46c160
commit d881cac768
123 changed files with 12961 additions and 60 deletions

View File

@ -0,0 +1,172 @@
/*
Copyright 2011 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google.Apis.Util;
using System;
using System.Globalization;
namespace Google.Apis.Logging
{
/// <summary>
/// An abstract base logger, upon which real loggers may be built.
/// </summary>
public abstract class BaseLogger : ILogger
{
// Does not match gRPC datetime log format, which is "MMdd HH:mm:ss.ffffff"
private const string DateTimeFormatString = "yyyy-MM-dd HH:mm:ss.ffffff";
/// <summary>
/// Construct a <see cref="BaseLogger"/>.
/// </summary>
/// <param name="minimumLogLevel">Logging will be enabled at this level and all higher levels.</param>
/// <param name="clock">The <see cref="IClock"/> to use to timestamp log entries.</param>
/// <param name="forType">The type from which entries are being logged. May be <c>null</c>.</param>
protected BaseLogger(LogLevel minimumLogLevel, IClock clock, Type forType)
{
MinimumLogLevel = minimumLogLevel;
IsDebugEnabled = minimumLogLevel <= LogLevel.Debug;
IsInfoEnabled = minimumLogLevel <= LogLevel.Info;
IsWarningEnabled = minimumLogLevel <= LogLevel.Warning;
IsErrorEnabled = minimumLogLevel <= LogLevel.Error;
Clock = clock ?? SystemClock.Default;
LoggerForType = forType;
if (forType != null)
{
var namespaceStr = forType.Namespace ?? "";
if (namespaceStr.Length > 0)
{
namespaceStr += ".";
}
_loggerForTypeString = namespaceStr + forType.Name + " ";
}
else
{
_loggerForTypeString = "";
}
}
private readonly string _loggerForTypeString;
/// <summary>
/// The <see cref="IClock"/> being used to timestamp log entries.
/// </summary>
public IClock Clock { get; }
/// <summary>
/// The type from which entries are being logged. May be <c>null</c>.
/// </summary>
public Type LoggerForType { get; }
/// <summary>
/// Logging is enabled at this level and all higher levels.
/// </summary>
public LogLevel MinimumLogLevel { get; }
/// <summary>
/// Is Debug level logging enabled?
/// </summary>
public bool IsDebugEnabled { get; }
/// <summary>
/// Is info level logging enabled?
/// </summary>
public bool IsInfoEnabled { get; }
/// <summary>
/// Is warning level logging enabled?
/// </summary>
public bool IsWarningEnabled { get; }
/// <summary>
/// Is error level logging enabled?
/// </summary>
public bool IsErrorEnabled { get; }
/// <summary>
/// Build a new logger of the derived concrete type, for use to log from the specified type.
/// </summary>
/// <param name="type">The type from which entries are being logged.</param>
/// <returns>A new <see cref="ILogger"/> instance, logging from the specified type.</returns>
protected abstract ILogger BuildNewLogger(Type type);
/// <inheritdoc/>
public ILogger ForType<T>() => ForType(typeof(T));
/// <inheritdoc/>
public ILogger ForType(Type type) => type == LoggerForType ? this : BuildNewLogger(type);
/// <summary>
/// Perform the actual logging.
/// </summary>
/// <param name="logLevel">The <see cref="LogLevel"/> of this log entry.</param>
/// <param name="formattedMessage">The fully formatted log message, ready for logging.</param>
protected abstract void Log(LogLevel logLevel, string formattedMessage);
private string FormatLogEntry(string severityString, string message, params object[] formatArgs)
{
var msg = string.Format(message, formatArgs);
var when = Clock.UtcNow.ToString(DateTimeFormatString, CultureInfo.InvariantCulture);
// Matches gRPC log format
return $"{severityString}{when} {_loggerForTypeString}{msg}";
}
/// <inheritdoc/>
public void Debug(string message, params object[] formatArgs)
{
if (IsDebugEnabled)
{
Log(LogLevel.Debug, FormatLogEntry("D", message, formatArgs));
}
}
/// <inheritdoc/>
public void Info(string message, params object[] formatArgs)
{
if (IsInfoEnabled)
{
Log(LogLevel.Info, FormatLogEntry("I", message, formatArgs));
}
}
/// <inheritdoc/>
public void Warning(string message, params object[] formatArgs)
{
if (IsWarningEnabled)
{
Log(LogLevel.Warning, FormatLogEntry("W", message, formatArgs));
}
}
/// <inheritdoc/>
public void Error(Exception exception, string message, params object[] formatArgs)
{
if (IsErrorEnabled)
{
Log(LogLevel.Error, $"{FormatLogEntry("E", message, formatArgs)} {exception}");
}
}
/// <inheritdoc/>
public void Error(string message, params object[] formatArgs)
{
if (IsErrorEnabled)
{
Log(LogLevel.Error, FormatLogEntry("E", message, formatArgs));
}
}
}
}

View File

@ -0,0 +1,54 @@
/*
Copyright 2017 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google.Apis.Util;
using System;
namespace Google.Apis.Logging
{
/// <summary>
/// A logger than logs to StdError or StdOut.
/// </summary>
public sealed class ConsoleLogger : BaseLogger, ILogger
{
/// <summary>
/// Construct a <see cref="ConsoleLogger"/>.
/// </summary>
/// <param name="minimumLogLevel">Logging will be enabled at this level and all higher levels.</param>
/// <param name="logToStdOut"><c>true</c> to log to StdOut, defaults to logging to StdError.</param>
/// <param name="clock">Optional <see cref="IClock"/>; will use the system clock if <c>null</c>.</param>
public ConsoleLogger(LogLevel minimumLogLevel, bool logToStdOut = false, IClock clock = null) : this(minimumLogLevel, logToStdOut, clock, null) { }
private ConsoleLogger(LogLevel minimumLogLevel, bool logToStdOut, IClock clock, Type forType) : base(minimumLogLevel, clock, forType)
{
LogToStdOut = logToStdOut;
}
/// <summary>
/// <c>false</c> to log to StdError; <c>true</c> to log to StdOut.
/// </summary>
public bool LogToStdOut { get; }
/// <inheritdoc/>
protected override ILogger BuildNewLogger(Type type) => new ConsoleLogger(MinimumLogLevel, LogToStdOut, Clock, type);
/// <inheritdoc/>
protected override void Log(LogLevel logLevel, string formattedMessage)
{
(LogToStdOut ? Console.Out : Console.Error).WriteLine(formattedMessage);
}
}
}

View File

@ -0,0 +1,62 @@
/*
Copyright 2011 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
namespace Google.Apis.Logging
{
/// <summary>Describes a logging interface which is used for outputting messages.</summary>
public interface ILogger
{
/// <summary>Gets an indication whether debug output is logged or not.</summary>
bool IsDebugEnabled { get; }
/// <summary>Returns a logger which will be associated with the specified type.</summary>
/// <param name="type">Type to which this logger belongs.</param>
/// <returns>A type-associated logger.</returns>
ILogger ForType(Type type);
/// <summary>Returns a logger which will be associated with the specified type.</summary>
/// <returns>A type-associated logger.</returns>
ILogger ForType<T>();
/// <summary>Logs a debug message.</summary>
/// <param name="message">The message to log.</param>
/// <param name="formatArgs">String.Format arguments (if applicable).</param>
void Debug(string message, params object[] formatArgs);
/// <summary>Logs an info message.</summary>
/// <param name="message">The message to log.</param>
/// <param name="formatArgs">String.Format arguments (if applicable).</param>
void Info(string message, params object[] formatArgs);
/// <summary>Logs a warning.</summary>
/// <param name="message">The message to log.</param>
/// <param name="formatArgs">String.Format arguments (if applicable).</param>
void Warning(string message, params object[] formatArgs);
/// <summary>Logs an error message resulting from an exception.</summary>
/// <param name="exception"></param>
/// <param name="message">The message to log.</param>
/// <param name="formatArgs">String.Format arguments (if applicable).</param>
void Error(Exception exception, string message, params object[] formatArgs);
/// <summary>Logs an error message.</summary>
/// <param name="message">The message to log.</param>
/// <param name="formatArgs">String.Format arguments (if applicable).</param>
void Error(string message, params object[] formatArgs);
}
}

View File

@ -0,0 +1,54 @@
/*
Copyright 2011 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace Google.Apis.Logging
{
/// <summary>
/// The supported logging levels.
/// </summary>
public enum LogLevel
{
/// <summary>
/// A value lower than all logging levels.
/// </summary>
All = 0,
/// <summary>
/// Debug logging.
/// </summary>
Debug = 100,
/// <summary>
/// Info logging.
/// </summary>
Info = 200,
/// <summary>
/// Warning logging.
/// </summary>
Warning = 300,
/// <summary>
/// Error logging.
/// </summary>
Error = 400,
/// <summary>
/// A value higher than all logging levels.
/// </summary>
None = 1000,
}
}

View File

@ -0,0 +1,72 @@
/*
Copyright 2011 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google.Apis.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Google.Apis.Logging
{
/// <summary>
/// A logger than logs to an in-memory buffer.
/// Generally for use during tests.
/// </summary>
public sealed class MemoryLogger : BaseLogger, ILogger
{
/// <summary>
/// Construct a <see cref="MemoryLogger"/>.
/// </summary>
/// <param name="minimumLogLevel">Logging will be enabled at this level and all higher levels.</param>
/// <param name="maximumEntryCount">The maximum number of log entries. Further log entries will be silently discarded.</param>
/// <param name="clock">Optional <see cref="IClock"/>; will use the system clock if <c>null</c>.</param>
public MemoryLogger(LogLevel minimumLogLevel, int maximumEntryCount = 1000, IClock clock = null) :
this(minimumLogLevel, maximumEntryCount, clock, new List<string>(), null) { }
private MemoryLogger(LogLevel minimumLogLevel, int maximumEntryCount, IClock clock, List<string> logEntries, Type forType) : base(minimumLogLevel, clock, forType)
{
_logEntries = logEntries;
LogEntries = new ReadOnlyCollection<string>(_logEntries);
_maximumEntryCount = maximumEntryCount;
}
private readonly int _maximumEntryCount;
// This list is shared between all derived MemoryLogger instances
private readonly List<string> _logEntries;
/// <summary>
/// The list of log entries.
/// </summary>
public IList<string> LogEntries { get; }
/// <inheritdoc/>
protected override ILogger BuildNewLogger(Type type) => new MemoryLogger(MinimumLogLevel, _maximumEntryCount, Clock, _logEntries, type);
/// <inheritdoc/>
protected override void Log(LogLevel logLevel, string formattedMessage)
{
lock (_logEntries)
{
if (_logEntries.Count < _maximumEntryCount)
{
_logEntries.Add(formattedMessage);
}
}
}
}
}

View File

@ -0,0 +1,59 @@
/*
Copyright 2011 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
namespace Google.Apis.Logging
{
/// <summary>
/// Represents a NullLogger which does not do any logging.
/// </summary>
public class NullLogger : ILogger
{
/// <inheritdoc/>
public bool IsDebugEnabled
{
get { return false; }
}
/// <inheritdoc/>
public ILogger ForType(Type type)
{
return new NullLogger();
}
/// <inheritdoc/>
public ILogger ForType<T>()
{
return new NullLogger();
}
/// <inheritdoc/>
public void Info(string message, params object[] formatArgs) {}
/// <inheritdoc/>
public void Warning(string message, params object[] formatArgs) {}
/// <inheritdoc/>
public void Debug(string message, params object[] formatArgs) {}
/// <inheritdoc/>
public void Error(Exception exception, string message, params object[] formatArgs) {}
/// <inheritdoc/>
public void Error(string message, params object[] formatArgs) {}
}
}