Skip to content

Commit

Permalink
Use High Performance Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado committed Feb 22, 2025
1 parent b32de41 commit 7288490
Show file tree
Hide file tree
Showing 116 changed files with 10,831 additions and 1,725 deletions.
69 changes: 69 additions & 0 deletions src/app/AppLoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Net;
using Microsoft.Extensions.Logging;

namespace SIPSorcery.SIP.App
{
internal static partial class AppLoggingExtensions
{
[LoggerMessage(
EventId = 0,
EventName = "SDPMangledForRequest",
Level = LogLevel.Debug,
Message = "SDP mangled for {Status} response from {RemoteSIPEndPoint}, adjusted address {RemoteEndPointAddress}.")]
public static partial void LogSdpMangledRequest(
this ILogger logger,
SIPMethodsEnum status,
SIPEndPoint remoteSipEndPoint,
string remoteEndPointAddress);

[LoggerMessage(
EventId = 0,
EventName = "SDPMangledForResponse",
Level = LogLevel.Debug,
Message = "SDP mangled for {Status} response from {RemoteSIPEndPoint}, adjusted address {RemoteEndPointAddress}.")]
public static partial void LogSdpMangledResponse(
this ILogger logger,
SIPResponseStatusCodesEnum status,
SIPEndPoint remoteSipEndPoint,
IPAddress remoteEndPointAddress);

[LoggerMessage(
EventId = 0,
EventName = "SdpMangleEmptyValues",
Level = LogLevel.Warning,
Message = "Mangle SDP was called with an empty body or public IP address.")]
public static partial void LogSdpMangleEmptyValues(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "SdpMangleException",
Level = LogLevel.Error,
Message = "Exception MangleSDP. {errorMessage}")]
public static partial void LogSdpMangleError(
this ILogger logger,
string errorMessage,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "SipRequestMangleException",
Level = LogLevel.Error,
Message = "Exception MangleSDP. {errorMessage}")]
public static partial void LogSipRequestMangleError(
this ILogger logger,
string errorMessage,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "SipResponseMangleException",
Level = LogLevel.Error,
Message = "Exception MangleSIPResponse. {errorMessage}")]
public static partial void LogSipResponseMangleError(
this ILogger logger,
string errorMessage,
Exception exception);
}
}
3 changes: 1 addition & 2 deletions src/app/Media/AudioSendOnlyMediaSession.cs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SIPSorcery.Net;
using SIPSorcery.SIP.App;
using SIPSorceryMedia.Abstractions;
Expand Down Expand Up @@ -35,7 +34,7 @@ public AudioSendOnlyMediaSession(
private void AudioFormatsNegotiated(List<AudioFormat> audioFormats)
{
var audioFormat = audioFormats.First();
logger.LogDebug("Setting audio source format to {AudioFormatID}:{AudioFormatCodec}.", audioFormat.FormatID, audioFormat.Codec);
logger.LogSettingAudioSourceFormat(audioFormat.FormatID, audioFormat.Codec);
AudioExtrasSource.SetAudioSourceFormat(audioFormat);
}

Expand Down
196 changes: 196 additions & 0 deletions src/app/Media/MediaLoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System;
using System.Net;
using Microsoft.Extensions.Logging;
using SIPSorceryMedia.Abstractions;

namespace SIPSorcery.Media
{
internal static partial class MediaLoggingExtensions
{
[LoggerMessage(
EventId = 0,
EventName = "SettingAudioSourceFormat",
Level = LogLevel.Debug,
Message = "Setting audio source format to {AudioFormatID}:{AudioFormatCodec}.")]
public static partial void LogSettingAudioSourceFormat(
this ILogger logger,
int audioFormatID,
AudioCodecsEnum audioFormatCodec);

[LoggerMessage(
EventId = 0,
EventName = "MusicFileNotSetOrFound",
Level = LogLevel.Warning,
Message = "Music file not set or not found, using default music resource.")]
public static partial void LogMusicFileNotSetOrFound(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "SendingAudioSteamLength",
Level = LogLevel.Debug,
Message = "Sending audio stream length {AudioStreamLength}.")]
public static partial void LogSendingAudioSteamLength(
this ILogger logger,
long audioStreamLength);

[LoggerMessage(
EventId = 0,
EventName = "RtpMediaPacketReceived",
Level = LogLevel.Trace,
Message = "audio RTP packet received from {RemoteEndPoint} ssrc {SyncSource} seqnum {SequenceNumber} timestamp {Timestamp} payload type {PayloadType}.")]
public static partial void LogRtpMediaPacketReceived(
this ILogger logger,
IPEndPoint remoteEndPoint,
uint syncSource,
ushort sequenceNumber,
uint timestamp,
int payloadType);

[LoggerMessage(
EventId = 0,
EventName = "SendAudioFromStreamCompleted",
Level = LogLevel.Debug,
Message = "Send audio from stream completed.")]
public static partial void LogSendAudioFromStreamCompleted(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "SettingAudioFormat",
Level = LogLevel.Debug,
Message = "Setting audio source format to {FormatID}:{Codec} {ClockRate} (RTP clock rate {RtpClockRate}).")]
public static partial void LogSettingAudioFormat(
this ILogger logger,
int formatID,
AudioCodecsEnum codec,
int clockRate,
int rtpClockRate);

[LoggerMessage(
EventId = 0,
EventName = "SettingVideoFormat",
Level = LogLevel.Debug,
Message = "Setting video sink and source format to {VideoFormatID}:{VideoCodec}.")]
public static partial void LogSettingVideoFormat(
this ILogger logger,
int videoFormatID,
VideoCodecsEnum videoCodec);

[LoggerMessage(
EventId = 0,
EventName = "AudioTrackDtmfNegotiated",
Level = LogLevel.Debug,
Message = "Audio track negotiated DTMF payload ID {AudioStreamNegotiatedRtpEventPayloadID}.")]
public static partial void LogAudioTrackDtmfNegotiated(
this ILogger logger,
int audioStreamNegotiatedRtpEventPayloadID);

[LoggerMessage(
EventId = 0,
EventName = "VideoCaptureDeviceFailure",
Level = LogLevel.Warning,
Message = "Video source for capture device failure. {errorMessage}")]
public static partial void LogVideoCaptureDeviceFailure(
this ILogger logger,
string errorMessage);

[LoggerMessage(
EventId = 0,
EventName = "WebcamVideoSourceFailed",
Level = LogLevel.Warning,
Message = "Webcam video source failed before start, switching to test pattern source.")]
public static partial void LogWebcamFailedSwitchingToPattern(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "StreamClosedWarning",
Level = LogLevel.Warning,
Message = "Stream Closed.")]
public static partial void LogStreamClosedWarning(
this ILogger logger,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "AudioStreamReadError",
Level = LogLevel.Warning,
Message = "Failed to read from audio stream source.")]
public static partial void LogAudioStreamReadError(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "AudioStreamNullError",
Level = LogLevel.Warning,
Message = "Failed to read from audio stream source, stream null or closed.")]
public static partial void LogAudioStreamNullError(
this ILogger logger);

[LoggerMessage(
EventId = 0,
EventName = "UnhandledStreamException",
Level = LogLevel.Warning,
Message = "Caught unhandled exception")]
public static partial void LogUnhandledStreamException(
this ILogger logger,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "StreamReaderCloseError",
Level = LogLevel.Warning,
Message = "Error occurred whilst trying to close the stream source reader.")]
public static partial void LogStreamReaderCloseError(
this ILogger logger,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "FrameRateError",
Level = LogLevel.Warning,
Message = "{framesPerSecond} fames per second not in the allowed range of {minimumFramesPerSecond} to {maximumFramesPerSecond}, ignoring.")]
public static partial void LogFrameRateError(
this ILogger logger,
int framesPerSecond,
int minimumFramesPerSecond,
int maximumFramesPerSecond);

[LoggerMessage(
EventId = 0,
EventName = "SilenceSampleError",
Level = LogLevel.Error,
Message = "Exception sending silence sample")]
public static partial void LogSendingSilenceSampleError(
this ILogger logger,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "SignalGeneratorError",
Level = LogLevel.Error,
Message = "Exception sending signal generator sample")]
public static partial void LogSignalGeneratorError(
this ILogger logger,
Exception exception);

[LoggerMessage(
EventId = 0,
EventName = "MediaEngineStartError",
Level = LogLevel.Error,
Message = "Error starting media engine. {ErrorMessage}")]
public static partial void LogMediaEngineStartError(
this ILogger logger,
string errorMessage,
Exception ex);

[LoggerMessage(
EventId = 0,
EventName = "MediaEngineStop",
Level = LogLevel.Debug,
Message = "Media engine stopped")]
public static partial void LogMediaEngineStop(
this ILogger logger);
}
}
20 changes: 10 additions & 10 deletions src/app/Media/Sources/AudioExtrasSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public void SetSource(AudioSourceOptions sourceOptions)
{
if (!string.IsNullOrWhiteSpace(_audioOpts.MusicFile))
{
Log.LogWarning("Music file not set or not found, using default music resource.");
Log.LogMusicFileNotSetOrFound();
}

var assem = typeof(VideoTestPatternSource).GetTypeInfo().Assembly;
Expand Down Expand Up @@ -381,7 +381,7 @@ private void InitialiseSendAudioFromStreamTimer(Stream audioStream, AudioSamplin
{
if (!_isClosed && audioStream != null && audioStream.Length > 0)
{
Log.LogDebug("Sending audio stream length {AudioStreamLength}.", audioStream.Length);
Log.LogSendingAudioSteamLength(audioStream.Length);

_streamSendInProgress = true;
_streamSourceRate = streamSampleRate;
Expand Down Expand Up @@ -418,7 +418,7 @@ private void SendMusicSample(object state)
}
catch (Exception e)
{
Log.LogWarning(e, "Stream Closed.");
Log.LogStreamClosedWarning(e);
}
}

Expand All @@ -440,7 +440,7 @@ private void SendSilenceSample(object state)
}
catch (Exception e)
{
Log.LogError(e, "Exception sending silence sample");
Log.LogSendingSilenceSampleError(e);
}
}

Expand All @@ -466,7 +466,7 @@ private void SendSignalGeneratorSample(object state)
}
catch (Exception e)
{
Log.LogError(e, "Exception sending signal generator sample");
Log.LogSignalGeneratorError(e);
}
}

Expand All @@ -491,27 +491,27 @@ private void SendStreamSample(object state)

if (_streamSourceReader.BaseStream.Position >= _streamSourceReader.BaseStream.Length)
{
Log.LogDebug("Send audio from stream completed.");
Log.LogSendAudioFromStreamCompleted();
StopSendFromAudioStream();
}
}
else
{
Log.LogWarning("Failed to read from audio stream source.");
Log.LogAudioStreamReadError();
StopSendFromAudioStream();
}
}
else
{
Log.LogWarning("Failed to read from audio stream source, stream null or closed.");
Log.LogAudioStreamNullError();
StopSendFromAudioStream();
}
}

}
catch (Exception e)
{
Log.LogWarning(e, "Caught unhandled exception");
Log.LogUnhandledStreamException(e);
StopSendFromAudioStream();
}

Expand All @@ -527,7 +527,7 @@ private void SendStreamSample(object state)
}
catch (Exception e)
{
Log.LogWarning(e, "Error occurred whilst trying to close the stream source reader.");
Log.LogStreamReaderCloseError(e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/Media/Sources/VideoTestPatternSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void SetFrameRate(int framesPerSecond)
{
if (framesPerSecond < MINIMUM_FRAMES_PER_SECOND || framesPerSecond > MAXIMUM_FRAMES_PER_SECOND)
{
logger.LogWarning("{FramesPerSecond} fames per second not in the allowed range of {MinimumFramesPerSecond} to {MaximumFramesPerSecond}, ignoring.", framesPerSecond, MINIMUM_FRAMES_PER_SECOND, MAXIMUM_FRAMES_PER_SECOND);
logger.LogFrameRateError(framesPerSecond, MINIMUM_FRAMES_PER_SECOND, MAXIMUM_FRAMES_PER_SECOND);
}
else
{
Expand Down
Loading

0 comments on commit 7288490

Please sign in to comment.