~ubuntu-branches/ubuntu/feisty/nant/feisty

« back to all changes in this revision

Viewing changes to src/NAnt.Core/Log.cs

  • Committer: Bazaar Package Importer
  • Author(s): Dave Beckett
  • Date: 2006-06-12 23:30:36 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060612233036-a1uwh0949z0218ep
Tags: 0.84+0.85-rc4-1
* New upstream release
* Acknowledge NMU (Closes: #372588)
* Standards-Version 3.7.2
* Update to latest CLI policy package split.  Build-Depends-Indep: on
  cli-common-dev, libmono-winforms1.0-cil, libmono-winforms2.0-cil and
  mono-gmcs to get 1.0 and 2.0 packages
* Removed patches no longer needed:
  - 01-AssemblyInfoTask.cs.patch
  - 02-ScriptTask.cs.patch
  - 03-XmlResultFormatter.cs.patch
  - 04-SourceControl.patch
  - 05-ExceptionTest.cs

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
using System.Text;
37
37
using System.Web.Mail;
38
38
 
 
39
using NAnt.Core.Types;
39
40
using NAnt.Core.Util;
40
41
 
41
42
namespace NAnt.Core {
787
788
    ///         <description>The subject of build success messages. [default: "Build Success"]</description>
788
789
    ///     </item>
789
790
    ///     <item>
 
791
    ///         <term>MailLogger.success.attachments</term>
 
792
    ///         <description>The ID of a fileset holdng set of files to attach when the build is successful.</description>
 
793
    ///     </item>
 
794
    ///     <item>
 
795
    ///         <term>MailLogger.failure.attachments</term>
 
796
    ///         <description>The ID of a fileset holdng set of files to attach when the build fails.</description>
 
797
    ///     </item>
 
798
    ///     <item>
790
799
    ///         <term>MailLogger.body.encoding</term>
791
800
    ///         <description>The encoding type of the body of the e-mail message. [default: system's ANSI code page]</description>
792
801
    ///     </item>
 
802
    ///     <item>
 
803
    ///         <term>MailLogger.smtp.username</term>
 
804
    ///         <description>The name of the user to login to the SMTP server.</description>
 
805
    ///     </item>
 
806
    ///     <item>
 
807
    ///         <term>MailLogger.smtp.password</term>
 
808
    ///         <description>The password of the specified user.</description>
 
809
    ///     </item>
 
810
    ///     <item>
 
811
    ///         <term>MailLogger.smtp.enablessl</term>
 
812
    ///         <description>Specifies whether to use SSL to encrypt the connection. [default: false]</description>
 
813
    ///     </item>
 
814
    ///     <item>
 
815
    ///         <term>MailLogger.smtp.port</term>
 
816
    ///         <description>The SMTP server port to connect to. [default: 25]</description>
 
817
    ///     </item>
793
818
    /// </list>
794
819
    /// </remarks>
795
820
    [Serializable()]
830
855
        /// <param name="sender">The source of the event.</param>
831
856
        /// <param name="e">A <see cref="BuildEventArgs" /> object that contains the event data.</param>
832
857
        public override void BuildFinished(object sender, BuildEventArgs e) {
 
858
#if (NET_1_1 || MONO_1_0)
 
859
            const string cdoNamespaceURI = "http://schemas.microsoft.com/cdo/configuration/";
 
860
#endif
 
861
 
833
862
            base.BuildFinished(sender, e);
834
863
 
835
864
            // remove an item from the project stack
850
879
            string prefix = success ? "success" : "failure";
851
880
 
852
881
            try {
853
 
                string propertyValue = GetPropertyValue(properties, prefix + ".notify", "true");
 
882
                string propertyValue = GetPropertyValue(properties, prefix + ".notify", "true", false);
854
883
 
855
884
                bool notify = true;
856
885
 
860
889
                    notify = true;
861
890
                }
862
891
 
863
 
                propertyValue = GetPropertyValue(properties, "body.encoding", "");
 
892
                propertyValue = GetPropertyValue(properties, "body.encoding", null, false);
864
893
 
865
894
                try {
866
 
                    if (propertyValue != null && propertyValue.Length > 0) {
 
895
                    if (propertyValue != null) {
867
896
                        bodyEncoding = Encoding.GetEncoding(propertyValue);
868
897
                    }
869
898
                } catch {
874
903
                    return;
875
904
                }
876
905
 
 
906
                // create message to send
877
907
                MailMessage mailMessage = new MailMessage();
878
 
                mailMessage.From = GetPropertyValue(properties, "from", null);
879
 
                mailMessage.To = GetPropertyValue(properties, prefix + ".to", null);
 
908
                mailMessage.From = GetPropertyValue(properties, "from", null, true);
 
909
                mailMessage.To = GetPropertyValue(properties, prefix + ".to", null, true);
880
910
                mailMessage.Subject = GetPropertyValue(properties, prefix + ".subject",
881
 
                    (success) ? "Build Success" : "Build Failure");
 
911
                    (success) ? "Build Success" : "Build Failure", false);
882
912
                mailMessage.Body = _buffer.ToString();
883
913
 
 
914
                string smtpUsername = GetPropertyValue(properties, "smtp.username", null, false);
 
915
                if (smtpUsername != null) {
 
916
#if (NET_1_1 || MONO_1_0)
 
917
                    mailMessage.Fields[cdoNamespaceURI + "smtpauthenticate"] = 1;
 
918
                    mailMessage.Fields[cdoNamespaceURI + "sendusername"] = smtpUsername;
 
919
#else
 
920
                    Console.Error.WriteLine("[MailLogger] MailLogger.smtp.username"
 
921
                        + " is not supported if NAnt is built targeting .NET"
 
922
                        + " Framework 1.0.");
 
923
#endif
 
924
                }
 
925
 
 
926
                string smtpPassword = GetPropertyValue(properties, "smtp.password", null, false);
 
927
                if (smtpPassword == null) {
 
928
#if (NET_1_1 || MONO_1_0)
 
929
                    mailMessage.Fields[cdoNamespaceURI + "sendpassword"] = smtpPassword;
 
930
#else
 
931
                    Console.Error.WriteLine("[MailLogger] MailLogger.smtp.password"
 
932
                        + " is not supported if NAnt is built targeting .NET"
 
933
                        + " Framework 1.0.");
 
934
#endif
 
935
                }
 
936
 
 
937
                string smtpPort = GetPropertyValue(properties, "smtp.port", null, false);
 
938
                if (smtpPort != null) {
 
939
#if (NET_1_1 || MONO_1_0)
 
940
                    mailMessage.Fields[cdoNamespaceURI + "smtpserverport"] = smtpPort;
 
941
#else
 
942
                    Console.Error.WriteLine("[MailLogger] MailLogger.smtp.port"
 
943
                        + " is not supported if NAnt is built targeting .NET"
 
944
                        + " Framework 1.0.");
 
945
#endif
 
946
                }
 
947
 
 
948
                string enableSSL = GetPropertyValue(properties, "smtp.enablessl", null, false);
 
949
                if (enableSSL != null) {
 
950
#if (NET_1_1 || MONO_1_0)
 
951
                    mailMessage.Fields[cdoNamespaceURI + "smtpusessl"] = enableSSL;
 
952
#else
 
953
                    Console.Error.WriteLine("[MailLogger] MailLogger.smtp.enablessl"
 
954
                        + " is not supported if NAnt is built targeting .NET"
 
955
                        + " Framework 1.0.");
 
956
#endif
 
957
                }
 
958
                
 
959
                // attach files in fileset to message
 
960
                AttachFiles(mailMessage, project, GetPropertyValue(properties, 
 
961
                    prefix + ".attachments", null, false));
 
962
 
 
963
                // set encoding of body
884
964
                if (bodyEncoding != null) {
885
965
                    mailMessage.BodyEncoding = bodyEncoding;
886
966
                }
887
967
 
888
968
                // send the message
889
 
                SmtpMail.SmtpServer = GetPropertyValue(properties, "mailhost", "localhost");
 
969
                SmtpMail.SmtpServer = GetPropertyValue(properties, "mailhost", "localhost", false);
890
970
                SmtpMail.Send(mailMessage);
891
971
            } catch (Exception ex) {
892
 
                Console.WriteLine("MailLogger failed to send e-mail!");
893
 
                Console.WriteLine(ex.ToString());
 
972
                Console.Error.WriteLine("[MailLogger] E-mail could not be sent!");
 
973
                Console.Error.WriteLine(ex.ToString());
894
974
            }
895
975
        }
896
976
 
912
992
        /// <param name="properties">Properties to obtain value from.</param>
913
993
        /// <param name="name">Suffix of property name.  "MailLogger" will be prepended internally.</param>
914
994
        /// <param name="defaultValue">Value returned if property is not present in <paramref name="properties" />.</param>
 
995
        /// <param name="required">Value indicating whether the property should exist, or have a default value set.</param>
915
996
        /// <returns>
916
997
        /// The value of the specified property; or the default value if the 
917
998
        /// property is not present in <paramref name="properties" />.
918
999
        /// </returns>
919
 
        /// <exception cref="ArgumentNullException">The specified property is not present and no default value has been given.</exception>
920
 
        private string GetPropertyValue(PropertyDictionary properties, string name, string defaultValue) {
 
1000
        /// <exception cref="ArgumentNullException"><paramref name="required" /> is <see langword="true" />, and the specified property is not present and no default value has been given.</exception>
 
1001
        private string GetPropertyValue(PropertyDictionary properties, string name, string defaultValue, bool required) {
921
1002
            string propertyName = "MailLogger." + name;
922
1003
            string value = (string) properties[propertyName];
923
1004
 
925
1006
                value = defaultValue;
926
1007
            }
927
1008
 
928
 
            if (value == null) {
 
1009
            if (required && value == null) {
929
1010
                throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Missing required parameter {0}.", propertyName));
930
1011
            }
931
1012
 
932
1013
            return value;
933
1014
        }
934
1015
 
 
1016
        private void AttachFiles(MailMessage mail, Project project, string filesetID) {
 
1017
            if (StringUtils.IsNullOrEmpty(filesetID)) {
 
1018
                return;
 
1019
            }
 
1020
 
 
1021
            // lookup fileset
 
1022
            FileSet fileset = project.DataTypeReferences[filesetID] as FileSet;
 
1023
            if (fileset == null) {
 
1024
                Console.Error.WriteLine("[MailLogger] Fileset \"{0}\" is not"
 
1025
                    + " defined. No files have been attached.", filesetID);
 
1026
                return;
 
1027
            }
 
1028
 
 
1029
            foreach (string fileName in fileset.FileNames) {
 
1030
                if (!File.Exists(fileName)) {
 
1031
                    Console.Error.WriteLine("[MailLogger] Attachment \"{0}\""
 
1032
                        + " does not exist. Skipping.", filesetID);
 
1033
                    continue;
 
1034
                }
 
1035
 
 
1036
                // create attachment
 
1037
                MailAttachment attachment = new MailAttachment(fileName, 
 
1038
                    MailEncoding.UUEncode);
 
1039
                // add attachment to mail
 
1040
                mail.Attachments.Add(attachment);
 
1041
            }
 
1042
        }
 
1043
 
935
1044
        #endregion Private Instance Methods
936
1045
 
937
1046
        #region Private Instance Fields
1196
1305
        }
1197
1306
 
1198
1307
        /// <summary>
1199
 
        /// Writes a character array to the text stream.
 
1308
        /// Writes a character array to the buffer.
1200
1309
        /// </summary>
1201
1310
        /// <param name="chars">The character array to write to the text stream.</param>
1202
1311
        public override void Write(char[] chars) {
1203
 
            _message += new string(chars, 0, chars.Length -1);
 
1312
            Write(new string(chars, 0, chars.Length -1));
1204
1313
        }
1205
1314
 
 
1315
        /// <summary>
 
1316
        /// Writes a string to the buffer.
 
1317
        /// </summary>
 
1318
        /// <param name="value"></param>
1206
1319
        public override void Write(string value) {
1207
1320
            _message += value;
1208
1321
        }
1209
1322
 
 
1323
        /// <summary>
 
1324
        /// Writes an empty string to the logging infrastructure.
 
1325
        /// </summary>
1210
1326
        public override void WriteLine() {
1211
1327
            WriteLine(string.Empty);
1212
1328
        }
1213
1329
 
1214
1330
 
1215
1331
        /// <summary>
1216
 
        /// Writes a string followed by a line terminator to the text stream.
 
1332
        /// Writes a string to the logging infrastructure.
1217
1333
        /// </summary>
1218
1334
        /// <param name="value">The string to write. If <paramref name="value" /> is a null reference, only the line termination characters are written.</param>
1219
1335
        public override void WriteLine(string value) {
1220
 
            _task.Log(OutputLevel, _message + value);
1221
 
            _message = string.Empty;
 
1336
            _message += value;
 
1337
            Flush();
1222
1338
        }
1223
1339
 
1224
1340
        /// <summary>
1228
1344
        /// <param name="line">The formatting string.</param>
1229
1345
        /// <param name="args">The object array to write into format string.</param>
1230
1346
        public override void WriteLine(string line, params object[] args) {
1231
 
            _task.Log(OutputLevel, _message + string.Format(
1232
 
                CultureInfo.InvariantCulture, line, args));
1233
 
            _message = string.Empty;
1234
 
        }   
 
1347
            _message += string.Format(CultureInfo.InvariantCulture, line, args);
 
1348
            Flush();
 
1349
        }
1235
1350
 
1236
 
        public override void Close() {
 
1351
        /// <summary>
 
1352
        /// Causes any buffered data to be written to the logging infrastructure.
 
1353
        /// </summary>
 
1354
        public override void Flush() {
1237
1355
            if (_message.Length != 0) {
1238
1356
                _task.Log(OutputLevel, _message);
1239
1357
                _message = string.Empty;
1240
1358
            }
 
1359
        }
 
1360
 
 
1361
        /// <summary>
 
1362
        /// Closes the current writer and releases any system resources 
 
1363
        /// associated with the writer.
 
1364
        /// </summary>
 
1365
        public override void Close() {
 
1366
            Flush();
1241
1367
            base.Close();
1242
1368
        }
1243
1369